How to get 304 from jQuery Ajax instead of 200?

My services return 304 , but jQuery Ajax seems to convert it to a 200 OK result.

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 see that the service returns 304 correctly.

Why does jQuery convert it to 200 ?

+4
source share
1 answer

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

or access to jqXHR in another way:

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

The correct way to handle 304 is not changed in jQuery ajax

change

optional ajax() setting:

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

but he interrupts the answer:

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 used it and it still works differently: - /

+3
source

Source: https://habr.com/ru/post/1412325/


All Articles