6

私のサービスはを返しますが304、jQueryAjaxはそれを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 
        }    
    }    
});

304Fiddlerを使用すると、サービスが正しく返されることがわかります。

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
        }    
    },    
)

jQueryajaxで変更されていない304を処理する適切な方法

編集

追加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 に答える