我正在使用 $.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.
只要您的网络服务器支持 Last-Modified
header ,那么对 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
如果网络服务器发现该文件自 If-Modified-Since
header 日期以来已被修改,它将正常提供该文件。
关于javascript - $.ajax() 调用,仅在更新时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3870566/