Resolve HTTP 304 - not modified in AJAX call made via GWT

前提是你 提交于 2019-12-25 16:39:44

问题


We are using an application made in GWT with the server as tomcat. The project runs fine normally, however there are situations where the server is restarted. At such point of time, the ajax call made by the code below returns blank text with the status code as 304

RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, URL.encode(serverUrl)); //-- serverUrl is the url to which this call is posted to.
requestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded");
requestBuilder.setHeader("Expires","0");
requestBuilder.sendRequest(
    postData, 
    new RequestCallback()
    {

        public void onError(Request request, Throwable exception)
        {
            //Do nothing
        }

        public void onResponseReceived(Request request, Response response)
        {
            //sometimes when the server is restarted, I get response.getStatusCode() = 304 and the response.getText() as blank
        }
    }
);

normally we get back some data from the server inside this response text. How do we now get the data when the response itself is blank ?


回答1:


HTTP 304 means that the server expects the client to use a cached copy of the page. The HTTP specification indicates that the response body should be empty the case of a 304 response (see see RFC 2616 section 10.3.5). Are you getting the 304 error because the server is messing up or because it really expects the response to be cached by the client? Perhaps try setting Cache-Control: no-cache in your headers.

If the 304 statuses are related to the server messing up, and you can't get them to stop, you have a couple options:

  1. Actually cache responses in your app so that when you get a 304 you can use the cached response
  2. Capture 304 responses and retry the RPC (perhaps recursively, specifying a retryTimesLeft as a parameter to the function)
  3. Indicate that 304's are actually failures and call some error handling function to deal with the lack of data

Which you choose obviously depends on the needs of your app.



来源:https://stackoverflow.com/questions/2475886/resolve-http-304-not-modified-in-ajax-call-made-via-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!