jQuery AJAX producing 304 responses when it shouldn't

前端 未结 3 1288
粉色の甜心
粉色の甜心 2020-12-14 06:53

This really has me scratching my head. Namely because it only happens in IE, not Firefox, and I was under the impression that jQuery was effectively browser neutral. I\'ve b

3条回答
  •  悲哀的现实
    2020-12-14 07:35

    I cannot see, you specifying the ajax request as a POST. So basically add:

    $.ajax({ type: 'POST' });
    

    and if that still fails (due to some browser AJAX weirdness), you could try setting cache: false:

    $.ajax({ type: 'POST', cache: false });
    

    Btw, all cache: false does, is adding some random stuff to the request URL.

    EDIT1:

    Regarding the

    ... and I was under the impression that jquery.ajax was designed to, unless otherwise instructed, not generate 304 responses

    jQuery istn't generating any responses here. And the 304-header is just an HTTP header. HTTP AJAX requests are ordinary HTTP requests and may return any valid header. If the server responds with 304, the XHR object will simply serve up the locally cached response from the server. It's completely transparent for the user, though.

    EDIT2:

    Removed the advice about preventing caching. Seems like Voodoo to me.

    EDIT3:

    Added that bit in again because it apparently was necessary. Looking around the web, IE seems to be illegally caching AJAX POSTs to some extent.

提交回复
热议问题