2012-05-14 16 views
4

私のサービスは304を返しますが、jQuery Ajaxは200 OKの結果に変換しているようです。200の代わりにjQuery Ajaxから304を取得するには?

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

フィドラーで、私はサービスが正しく304を返すことがわかります。

は、これが私の要求です。

jQueryはなぜそれを200に変換しますか?

+0

可能重複[ jQuery.ajax()リクエストのヘッダーステータスが "304 Not Modified"であるかどうかを確認してください。](http://stackoverflow.com/questions/5173656/how-to-check-if-jquery-ajax-request-header-status-is -304-not-modified) – apsillers

答えて

3

あなたは、それらを区別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` 

が、それはデータREPONSEを破るには:

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

しかし、あなたはそれを使用しましたし、それはまだ動作が異なります: - どのようにの/

+0

それでも200を返します。 –

+0

どのバージョンのjQueryとどのブラウザ? –

+0

1.7.2/Chrome最新バージョン/ Firefoxの最新バージョン –

関連する問題