Javascript 每隔刷新一次包含$.ajax()请求的页面,就会导致状态412错误
这个问题只发生在Safari for Mac上。 我正在加载干净的json数据文件,其中包含以下内容:Javascript 每隔刷新一次包含$.ajax()请求的页面,就会导致状态412错误,javascript,jquery,ajax,apache,http-status-code-412,Javascript,Jquery,Ajax,Apache,Http Status Code 412,这个问题只发生在Safari for Mac上。 我正在加载干净的json数据文件,其中包含以下内容: $.ajax({ url : url, type : 'POST', dataType : 'json', contentType : 'application/json; charset=utf-8', crossdomain: true, context : contextObj.context
$.ajax({
url : url,
type : 'POST',
dataType : 'json',
contentType : 'application/json; charset=utf-8',
crossdomain: true,
context : contextObj.context,
success : function(data) {
// Irrelevant
callBackHandler.call(contextObj.context, event);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, If-Modified-Since, If-None-Match, *"
Header set Access-Control-Max-Age "3600"
启用CORS的服务器具有以下功能:
$.ajax({
url : url,
type : 'POST',
dataType : 'json',
contentType : 'application/json; charset=utf-8',
crossdomain: true,
context : contextObj.context,
success : function(data) {
// Irrelevant
callBackHandler.call(contextObj.context, event);
},
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr.status);
console.log(thrownError);
}
});
Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, POST, OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, If-Modified-Since, If-None-Match, *"
Header set Access-Control-Max-Age "3600"
我捕获了请求头,我看到的唯一区别在于以下几点。需要明确的是,失败的请求中包含以下内容,而下一次刷新不包含以下内容。因此,将项添加到访问控制允许标头:
If-Modified-Since: Fri, 08 Mar 2013 15:57:07 GMT
If-None-Match: "6801d-c550-4d76be008fac0"
这是服务器配置问题、jQuery使用问题还是其他问题?我觉得奇怪的是,Safari在每次刷新时都会不断更改标题。Michael,问题显然是关于跨域AJAX的,但您也提到了页面刷新。请详细说明该场景。加载网页时,$.ajax()用于从另一个web服务器检索*.json文件。该对象用于渲染动画。试图通过浏览器手动刷新页面来重播动画时出现的问题。很明显,Mac上的Safari的行为与其他浏览器不同,但我不知道为什么它会在每一个其他请求上发送额外的头,或者如何在客户端或服务器上进行补偿。您在access control allow headers头中添加了通配符。这是不对的。中指定了正确的方法:我刚刚尝试从服务器设置中删除*,重新启动web服务-失败,重新启动系统-仍然失败。Safari成功请求和失败请求之间的差异似乎是一个线索;但是,我是否有正确的设置。。。即使是失败的请求?