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

Ale*_*ler 6 jquery

我的服务返回一个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 
        }    
    }    
});
Run Code Online (Sandbox Code Playgroud)

使用Fiddler,我可以看到服务304正确返回.

为什么jQuery将其转换为200

Jac*_*iuk 3

如果你想区分它们,请使用success(data, textStatus, jqXHR)事件并从中读取它jqXHR.status

或以其他方式访问 jqXHR:

var jqxhr = $.ajax(
    ...     
    statusCode: {    
        200: function() {    
            if(304 == jqxhr.status)
                alert("not modified"); // does not fire
        }    
    },    
)
Run Code Online (Sandbox Code Playgroud)

处理 jQuery ajax 中未修改的 304 的正确方法

编辑

附加ajax()设置:

ifModified:true, // Lets respond `304:notmodified`
Run Code Online (Sandbox Code Playgroud)

但它破坏了数据响应:

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

但你已经使用过它并且它的工作方式仍然不同:-/