解决HTTP 304-在通过GWT进行的AJAX调用中未修改 [英] Resolve HTTP 304 - not modified in AJAX call made via GWT

查看:122
本文介绍了解决HTTP 304-在通过GWT进行的AJAX调用中未修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用GWT制作的应用程序,服务器为tomcat. 项目正常运行正常,但是在某些情况下会重新启动服务器.在这样的时间点,下面的代码进行的ajax调用返回空白文本 状态代码为304

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 ?

推荐答案

HTTP 304表示服务器希望客户端使用页面的缓存副本. HTTP规范指示在304响应的情况下响应主体应为空(请参见请参阅RFC 2616第10.3.5节).您是由于服务器混乱还是因为它确实希望客户端缓存响应而收到304错误?也许尝试在标题中设置Cache-Control: no-cache .

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.

如果304种状态与服务器混乱有关,而您无法停止它们,则有两种选择:

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. 实际上在应用程序中缓存了响应,因此当您收到304时,您可以使用缓存的响应
  2. 捕获304响应并重试RPC(可能是递归的,将retryTimesLeft指定为函数的参数)
  3. 表明304实际上是故障,并调用一些错误处理函数来处理数据不足的情况
  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.

这篇关于解决HTTP 304-在通过GWT进行的AJAX调用中未修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆