2014-10-16 114 views
1

我有一个GWT模块,并在其中我通过导航到不同的URL:GWT处理请求错误

Window.Location.assign(url); 

的导航网址,然后通过一个servlet处理,直到这一点,如果有错误它是由resp.sendError方法处理的

resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed."); 

然后,它将导航到浏览器错误页面。不过,我想知道有没有我无法导航到错误页面?即我能够检查我的GWT代码是否有错误,然后做些什么?像重新发送请求等。

谢谢!

+0

你的问题不清楚。你可以说得更详细点吗 ? – Pintouch 2014-10-16 15:46:34

+0

情景不清楚或我所做的不清楚? – jonatzin 2014-10-16 15:53:03

回答

2

当你离开你的web应用程序就是这样。您应该制作一个HTTP request still from your webapplication,而不是使用Window.Location.assign,例如使用RequestBuilder。从前面提到的文档
例子:

import com.google.gwt.http.client.*; 
... 

String url = "http://www.myserver.com/getData?type=3"; 
RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(url)); 

try { 
    Request request = builder.sendRequest(null, new RequestCallback() { 
    public void onError(Request request, Throwable exception) { 
     // Couldn't connect to server (could be timeout, SOP violation, etc.) 
    } 

    public void onResponseReceived(Request request, Response response) { 
     if (200 == response.getStatusCode()) { 
      // Process the response in response.getText() 
     } else { 
     // Handle the error. Can get the status text from response.getStatusText() 
     } 
    } 
    }); 
} catch (RequestException e) { 
    // Couldn't connect to server 
} 

请注意,这只会工作,如果你的servlet和web应用在同一个地址(域名,端口,协议),因为Same Origin Policy。如果情况并非如此,那么仍然有some options,就像带有填充的JSON(GWT通过JsonpRequestBuilder支持)。

+0

谢谢伊戈尔我会测试它并让你知道 – jonatzin 2014-10-16 16:19:09