在 GWT 中,何时使用 JsonpRequestBuilder 在 json-p 请求中调用 AsyncCallback:onFailure 方法
我一直在实现一个调用 REST 服务(我们也在开发)的 GWT 应用程序。当 REST 服务返回 HTTP 状态不是 200 的任何内容时,我希望调用 AsyncCallback 的 onFailure 方法。但是我无法让这种情况发生。
为了进一步测试它,我创建了一个测试 GWT 应用程序和一个测试 servlet。 GWT 应用程序调用服务的部分如下所示:
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.setCallbackParam("_jsonp");
jsonp.setFailureCallbackParam("_jsonp_failure");
jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>()
{
@Override
public void onFailure(Throwable caught)
{
Window.alert("Failure: " + caught.getMessage());
}
@Override
public void onSuccess(JavaScriptObject result)
{
Window.alert("Success");
}
});
servlet 代码如下所示:
public class MyRestServlet extends HttpServlet
{
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
String padding = httpServletRequest.getParameter("_jsonp_failure");
httpServletResponse.setContentType("application/x+javascript");
httpServletResponse.setStatus(500);
PrintWriter out = httpServletResponse.getWriter();
out.println(padding + "({\"some\":\"json\"});");
out.close();
}
}
OnFailure 最终在请求超时时被调用,但我希望它在 http 响应到达后立即被调用(如果它是失败)。我想有些东西我还不明白,我真的很感激能得到一些帮助。
谢谢
I've been implementing a GWT application that calls a REST-service (which we're also developing). When the REST-service returns anything with a HTTP-status other than 200 I would expect the onFailure method of AsyncCallback to be called. However I can't get this to happen.
To test it further I created a test GWT app and a test servlet. The part of the GWT app that calls the service looks like this:
JsonpRequestBuilder jsonp = new JsonpRequestBuilder();
jsonp.setCallbackParam("_jsonp");
jsonp.setFailureCallbackParam("_jsonp_failure");
jsonp.requestObject(url, new AsyncCallback<JavaScriptObject>()
{
@Override
public void onFailure(Throwable caught)
{
Window.alert("Failure: " + caught.getMessage());
}
@Override
public void onSuccess(JavaScriptObject result)
{
Window.alert("Success");
}
});
The servlet-code looks like this:
public class MyRestServlet extends HttpServlet
{
protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException
{
String padding = httpServletRequest.getParameter("_jsonp_failure");
httpServletResponse.setContentType("application/x+javascript");
httpServletResponse.setStatus(500);
PrintWriter out = httpServletResponse.getWriter();
out.println(padding + "({\"some\":\"json\"});");
out.close();
}
}
OnFailure eventually gets called when the request times out, but I would expect it to be called as soon as the http response arrives(if it's a failure). I guess there is something I haven't understood and I would really appreciate to get some help with this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 HTML5 ,如果加载脚本时出错,则应调度
error
事件,并且 GWT 不会侦听该事件(因为几乎没有浏览器实际触发它 AFAICT)。为了获得最佳的浏览器兼容性,您最好始终发送 200 状态,然后调用失败回调(或者换句话说,返回错误状态/条件,而不是抛出异常)。
此外,失败回调的参数应该是一个字符串(将是异常的消息)。
According to HTML5, if there's an error loading the script, an
error
event should be dispatched, and GWT doesn't listen for it (because almost no browser actually fires it AFAICT).For best browser compatibility, you'd better always send a 200 status, but then call the failure callback (or in other words, return an error state/condition, rather than throw an exception).
Also, the argument to the failure callback is expected to be a string (will be the message of the exception).
在调用 REST 服务的服务器代码中,如果响应不是 200,请自行引发异常(通过编写代码自行检查响应)。这样,它将作为错误保留在客户端,并且 onFailure 将在客户端被调用。
目前 GWT 认为没有任何问题。它发送了一个请求,得到了一些结果,不管怎样,调用成功了。它确实会在超时时调用 onFailure,因为请求“物理上”确实出现了问题,并且 GWT 将异常作为失败保留在客户端。
From the server code where you call the REST service, throw an exception yourself if the response is something other than 200 (by writing code to check the response yourself). This way it will persist to the client side as an error and onFailure will be called in client side.
In GWT's mind currently nothing went wrong. It sent a request, got some result did not matter what, the call was successful. It does call the onFailure on a timeout because something did go wrong with the request "physically", and GWT persisted the exception to the client side as a failure.