$。阿贾克斯()调用,只在updation只在、阿贾克斯、updation

2023-09-10 19:40:05 作者:蝶恋花

我读使用$阿贾克斯()请求的XML。

I am reading a xml using $.ajax() request.

$.ajax({
    type: "GET",
    async: false,
    url: "../../../ErrorMessages.xml",
    dataType: "xml",
    success: function (xml) {
       $(xml).find("*[Name='" + Field + "']").each(function () {
          message = $(this).find(Rule).text();
       });
    }
});

我想拨打电话时,才将资源 ErrorMessages.xml 更新。否则使用浏览器的缓存。

I want to make the call only when, the resource ErrorMessages.xml is updated. else use the browser cache.

推荐答案

浏览器将不知道是否 ErrorMessages.xml 已在服务器上更新。它必须发出一个请求,以检查是否在文件已被修改。

The browser won't know if ErrorMessages.xml has been updated on the server. It has to issue a request to check if the file has been modified.

您可能想在你的jQuery $设置 ifModified 选项。阿贾克斯()的要求,因为这是设置为默认:

You may want to set the ifModified option to true in your jQuery $.ajax() request, since this is set to false by default:

$.ajax({
  type: "GET",
  ifModified: true,
  async: false,
  url: "../../../ErrorMessages.xml",
  dataType: "xml",
  success: function (xml) {
     // ..
  }
});

从 jQuery.ajax()文档:

默认值:false

允许请求是成功的只有响应自上次请求改变。这是通过检查Last-Modified头完成。默认值为false,忽略了头。在jQuery的1.4这一技术还检查服务器指定的'ETAG赶上未修改的数据。

Allow the request to be successful only if the response has changed since the last request. This is done by checking the Last-Modified header. Default value is false, ignoring the header. In jQuery 1.4 this technique also checks the 'etag' specified by the server to catch unmodified data.

只要你的网络服务器支持 的Last-Modified 头,则第一个请求到XML看起来是这样的:

As long as your web server supports the Last-Modified header, then the first request to the XML would look something like this:

GET /ErrorMessages.xml HTTP/1.1
Host: www.example.com

HTTP/1.1 200 OK
Last-Modified: Wed, 06 Oct 2010 08:20:58 GMT
Content-Length: 1234

不过,后续请求相同的资源将是这样的:

However, subsequent requests to the same resource will look like this:

GET /ErrorMessages.xml HTTP/1.1
Host: www.example.com
If-Modified-Since: Wed, 06 Oct 2010 08:20:58 GMT

HTTP/1.1 304 Not Modified

如果Web服务器发现该文件已被修改以来的如果修改 - 因为头日期,它通常会成为该文件。

If the web server finds that the file has been modified since the If-Modified-Since header date, it will serve the file normally.