6

My services returns a 304 but jQuery Ajax seems to convert it to a 200 OKresult.

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.

Why does jQuery convert it to a 200?

Alexander Zeitler
  • 11,919
  • 11
  • 81
  • 124
  • possible duplicate of [How to check if jQuery.ajax() request header Status is "304 Not Modified"?](http://stackoverflow.com/questions/5173656/how-to-check-if-jquery-ajax-request-header-status-is-304-not-modified) – apsillers Sep 24 '14 at 15:49

1 Answers1

3

If You want to distinguish between them, use success(data, textStatus, jqXHR) event and read it from jqXHR.status

or access jqXHR other way:

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

edit

additional ajax() setting:

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

but You've used it and it still works differently :-/

Community
  • 1
  • 1
Jacek Kaniuk
  • 5,229
  • 26
  • 28