Modify ↓
Ticket #8394 (closed bug: invalid)
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@…> | Owned by: | |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | ajax | Version: | 1.5.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
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 `undefined`
}
});
XHR first time returns ok:
200:success
Object {content="Hello!"}
but on next times returns data undefined:
304:notmodified
undefined
How to solve it? It seems jQuery 1.5.1 bug. Expected result:
304:notmodified
Object {content="Hello!"}
Change History
comment:1 Changed 2 years ago by jaubourg
- Status changed from new to closed
- Resolution set to invalid
comment:2 Changed 2 years ago by Binyamin <7raivis@…>
@jaubourg - Then how to get:
304:notmodified
Object {content="Hello!"}
when http://localhost/api returns 304 Not Modified?
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.
Note: See
TracTickets for help on using
tickets.

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 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.