我的服务返回 304
,但 jQuery Ajax 似乎将其转换为 200 OK
结果。
这是我的要求:
$.ajax({
url: '/api/items',
type: 'GET',
dataType: 'json',
contentType: 'application/json',
ifModified:true,
cache: false,
statusCode: {
304: function() {
alert("not modified"); // does not fire
}
},
complete: function (xhr, status) {
console.log(xhr.status); // 200
}
}
});
使用 Fiddler,我可以看到该服务正确返回 304
。
为什么 jQuery 将其转换为 200
?
最佳答案
如果您想区分它们,请使用 success(data, textStatus, jqXHR)
事件并从 jqXHR.status
读取它
或以其他方式访问jqXHR:
var jqxhr = $.ajax(
...
statusCode: {
200: function() {
if(304 == jqxhr.status)
alert("not modified"); // does not fire
}
},
)
Proper way to handle 304 not modified in jQuery ajax
编辑
附加ajax()
设置:
ifModified:true, // Lets respond `304:notmodified`
但它破坏了数据响应:
但是您已经使用了它,但它的工作方式仍然不同:-/
关于jquery - 如何从 jQuery Ajax 获取 304 而不是 200?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10579510/