简体   繁体   English

如何从jQuery Ajax而不是200获得304?

[英]How to get 304 from jQuery Ajax instead of 200?

My services returns a 304 but jQuery Ajax seems to convert it to a 200 OK result. 我的服务返回304但jQuery Ajax似乎将其转换为200 OK结果。

This is my request: 这是我的要求:

$.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 
        }    
    }    
});

With Fiddler I can see that the service returns 304 correctly. 使用Fiddler,我可以看到服务正确返回304

Why does jQuery convert it to a 200 ? 为什么jQuery将它转换为200

If You want to distinguish between them, use success(data, textStatus, jqXHR) event and read it from jqXHR.status 如果要区分它们,请使用success(data, textStatus, jqXHR)事件并从jqXHR.status读取它

or access jqXHR other way: 或以其他方式访问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 正确的方法来处理304未在jQuery ajax中修改

edit 编辑

additional ajax() setting: 额外的ajax()设置:

ifModified:true, // Lets respond `304:notmodified`

but it breaks data reponse: 但它打破了数据响应:

http://forum.jquery.com/topic/how-to-fix-browser-cache-and-notmodified-respond-for-json-jquery-ajax-ifmodified-true-break-on-data-respond http://forum.jquery.com/topic/how-to-fix-browser-cache-and-notmodified-respond-for-json-jquery-ajax-ifmodified-true-break-on-data-respond

but You've used it and it still works differently :-/ 但是你已经使用它了,它仍然有不同的作用: - /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM