处理未在 jQuery ajax 中修改的 304 的正确方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5215181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:45:22  来源:igfitidea点击:

Proper way to handle 304 not modified in jQuery ajax

jquerybrowser-cache

提问by Scott

As of jQuery 1.5, the ajax methods now correctly handle 304 Not Modified responses by calling the success() handler, as per the W3C spec for XMLHTTPRequest. This allows your application to treat the request as being successful, even if the server didn't actually return any data (because you already have the latest data cached).

从 jQuery 1.5 开始,ajax 方法现在通过调用 success() 处理程序正确处理 304 Not Modified 响应,根据 XMLHTTPRequest 的 W3C 规范。这允许您的应用程序将请求视为成功,即使服务器实际上没有返回任何数据(因为您已经缓存了最新数据)。

For a normal (uncached) GET request, the success handler is called with the following args:

对于普通(未缓存的)GET 请求,成功处理程序使用以下参数调用:

  • data: { the data from the server }
  • status: OK
  • jqXHR:
    • status: 200
    • statusText: OK
    • responseText: { the data from the server }
  • 数据:{来自服务器的数据}
  • 状态:好的
  • jqXHR:
    • 状态:200
    • 状态文本:好的
    • responseText: { 来自服务器的数据 }

For a cached GET request, the success handler is called with the following args:

对于缓存的 GET 请求,使用以下参数调用成功处理程序:

  • data: undefined
  • status: notmodified
  • jqXHR:
    • status: 304
    • statusText: notmodified
    • responseText: { the data from the cache }
  • 数据:未定义
  • 状态:未修改
  • jqXHR:
    • 状态:304
    • 状态文本:未修改
    • responseText: { 来自缓存的数据 }

(at least, this is how it is returned in IOS 4.2, for a web-app that uses the application cache via a manifest file. I'm assuming this is consistent for general browser caching on most platforms/browsers).

(至少,对于通过清单文件使用应用程序缓存的 Web 应用程序,这是在 IOS 4.2 中返回的方式。我假设这与大多数平台/浏览器上的一般浏览器缓存一致)。

You can see that the "data" argument is only populated if the request was 200 OK; where as the jqXHR.responseText is always populated with data, regardless of whether that data came from the server (200 OK) or from the cache (304 Not Modified).

您可以看到“data”参数仅在请求为 200 OK 时才填充;其中 jqXHR.responseText 始终填充数据,无论该数据来自服务器(200 OK)还是来自缓存(304 Not Modified)。

Given that, in most GET requests, your success handler is going to want to do something with the data you got regardless of where it came from, it would seem to make the most sense for your success code to always use the jqXHR.responseText, rather than doing something like this:

鉴于在大多数 GET 请求中,您的成功处理程序将想要对您获得的数据执行某些操作,而不管它来自何处,对于您的成功代码而言,始终使用 jqXHR.responseText 似乎最有意义,而不是做这样的事情:

if ("notmodified" === status) {
  // do something with jqXHR.responseText
} else {
  // do something with data
}

Or is there ever a case when jqXHR.responseText wouldn'tbe populated in the success handler, but the data arg would?

或者是否存在 jqXHR.responseText不会在成功处理程序中填充但数据 arg填充的情况

I have to go through my codebase and change all success handlers (previously I was on jQuery 1.4.2, which always returned data, even from the cache); so I just want to make sure I'm handling it the right way. (Don't wan't to get to the end and then realise I should have done it another way).

我必须检查我的代码库并更改所有成功处理程序(以前我使用的是 jQuery 1.4.2,它总是返回数据,甚至从缓存中);所以我只想确保我以正确的方式处理它。(不要不想走到最后然后意识到我应该用另一种方式来做)。

回答by Scott

I've just spotted the obvious flaw in my question....I was assuming that the data was always text, so using jqXHR.responseText in preference to the data argument made sense.

我刚刚在我的问题中发现了明显的缺陷......我假设数据总是文本,所以优先使用 jqXHR.responseText 而不是数据参数是有道理的。

But in the case that the dataType is JSON, JSONP, script etc...if the data returned in a 304 Not Modified response comes back as undefined, you'd need to convert the jqXHR.responseText from a string to the desired type first, eg.

但是在 dataType 是 JSON、JSONP、脚本等的情况下......如果在 304 Not Modified 响应中返回的数据返回未定义,您需要先将 jqXHR.responseText 从字符串转换为所需的类型,例如。

if (data === undefined) {
  data = $.parseJSON(jqXHR.responseText);
}

...and you'd only want to do this (potentially expensive) conversion when you need really to.

...并且您只想在真正需要时进行此(可能很昂贵)转换。

Kinda makes sense now that I think about it...data is always going to be what came back from the server (which in some cases might notbe undefined for a 304...eg. the server could return some additional text/html); which allows the developer the flexibility to choose what they want to do in the event of a 304, eg.

现在我想起来有点道理……数据总是从服务器返回的(在某些情况下,对于 304可能不是未定义的……例如,服务器可以返回一些额外的文本/html ); 这允许开发人员灵活地选择他们想要在 304 事件中做什么,例如。

  • Display the response from the server (if any)
  • Use the jqXHR.responseText
  • Do something else entirely...
  • 显示来自服务器的响应(如果有)
  • 使用 jqXHR.responseText
  • 完全做别的事情...

回答by David N. Welton

Depending on context, you could use the cacheparameter to the ajax request:

根据上下文,您可以将cache参数用于 ajax 请求:

$.ajax({
    url: ".....",
    dataType: "json",
    type: "GET",
        cache: false,
    contentType: "application/json",
        success: function (data, textStatus) {
            console.log("RECV: " + data);
        }
    });

That's working for me.

这对我有用。