6

我的服务返回一个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?

4

1 回答 1

3

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

或以其他方式访问 jqXHR:

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

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

编辑

附加ajax()设置:

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

但它破坏了数据响应:

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

但是您已经使用了它,但它的工作方式仍然不同:-/

于 2012-05-14T08:15:02.033 回答