微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript-$.ajax()调用,仅在更新时

我正在使用$.ajax()请求读取xml.

$.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更新时进行呼叫.否则使用浏览器缓存.

解决方法:

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

您可能需要在jQuery $.ajax()请求中将ifModified选项设置为true,因为认情况下将其设置为false:

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

jQuery.ajax() documentation报价:

ifModified (Boolean)

Default: false

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.

只要您的Web服务器支持Last-Modified标头,那么对XML的第一个请求将看起来像这样:

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

但是,随后对同一资源的请求将如下所示:

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-Modified-Since标头日期以来文件已被修改,它将正常处理文件.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐