Skip to main content

Bug Tracker

Side navigation

#8394 closed bug (invalid)

Opened February 26, 2011 08:21PM UTC

Closed February 27, 2011 04:20PM UTC

Last modified August 01, 2013 12:23PM UTC

How to fix browser cache and notmodified respond for JSON? jQuery.ajax({ifModified:true,cache:true}) JSON request break on data respond.

Reported by: Binyamin <7raivis@inbox.lv> Owned by:
Priority: low Milestone: 1.next
Component: ajax Version: 1.5.1
Keywords: Cc:
Blocked by: Blocking:
Description

How to fix browser cache and notmodified respond for JSON? jQuery.ajax({ifModified:true,cache:true}) JSON request break on data respond.

First time browser request http://localhost/api returns status 200 OK and nexts 304 Not Modified

    $.ajax({
        type:"GET",
        url:'http://localhost/api',
        dataType:'json',
        cache:true,
        ifModified:true,
        success:function(data,textStatus,jqXHR){
            console.debug(jqXHR.status+':'+textStatus);
            console.debug(data); // On repeated request returns 
        }
    });

XHR first time returns ok:

    200:success
    Object {content="Hello!"}

but on next times returns undefined **undefined**:

    304:notmodified
    undefined

How to solve it? It seems jQuery 1.5.1 bug. **Expected result:**

    304:notmodified
    Object {content="Hello!"}
Attachments (0)
Change History (6)

Changed February 27, 2011 04:20PM UTC by jaubourg comment:1

resolution: → invalid
status: newclosed

That's exactly how the ifModified option is supposed to work. Setting ifModified to true will have ajax set headers to issue a conditional request.

http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html, section 10.3.5, about 304 Not Modified:

If the client has performed a conditional request and access is allowed,
but the document has not been modified, the server SHOULD respond with this
status code. The 304 response MUST NOT contain a message-body, and thus is
always terminated by the first empty line after the header fields.

If you want to receive 200 OK responses all the time, then do NOT use the ifModified option: the browser's xhr implementation will deal with 304 Not Modified responses under the hood while only presenting 200 OK responses to user code.

Changed February 27, 2011 06:32PM UTC by Binyamin <7raivis@inbox.lv> comment:2

@jaubourg - Then how to get:

304:notmodified
    Object {content="Hello!"}

when http://localhost/api returns 304 Not Modified?

Changed February 27, 2011 09:09PM UTC by dmethvin comment:3

@Binyamin: Cache it in your own code. If you need more help, ask on forum.jquery.com since this is not a bug.

Changed February 28, 2011 11:21AM UTC by jitter comment:4

component: unfiledajax
priority: undecidedlow

Changed August 01, 2013 11:25AM UTC by artemciy@gmail.com comment:5

There is a bug in jQuery.

Consider the following request:

var x = new XMLHttpRequest();

x.open ('GET', 'http://j2.bc24.ru/', false);

x.send (null);

console.log (x.responseText);

When it rans for the second time, it will return the cached content from the browser, as it should per the specification: "For 304 Not Modified responses that are a result of a user agent generated conditional request the user agent must act as if the server gave a 200 OK response with the appropriate content."

A similar jQuery request:

jQuery.ajax ('http://j2.bc24.ru/', {async: false, success (data) {console.log (data)}})

will return an empty data. The browser has cached the response, but jQuery failed to provide it, rendering jQuery.ajax useless around any kind of HTTP caching.

Changed August 01, 2013 12:23PM UTC by artemciy@gmail.com comment:6

Please disregard what I've said earlier. jQuery works fine, it was my error.