1

How do I make sure in jQuery that server responds with 304 only after a successful 200 request.

I am seeing this issue where client (browser which is making a jquery.ajax() call ) closes a connection, but subsequent requests from client to server result in 304 response code.

The file being served is a static file.

user462455
  • 12,838
  • 18
  • 65
  • 96
  • It sounds like the file is being cached - do you not want this? – thatidiotguy Nov 19 '13 at 18:25
  • Is this [post](http://stackoverflow.com/a/10579657/623952) helpful at all? – gloomy.penguin Nov 19 '13 at 18:25
  • Hey @thatidiotguy, yes the file is being cached but I doubt that full file is served in first request. My guess is that client closed connection during download so doesn't have full file. But subsequent requests read that incomplete cache file. Is it possible to disable that on client/server side – user462455 Nov 19 '13 at 18:27

1 Answers1

2

That would be the browser cache resulting in a 304 Not Modified header when doing an ajax call to the same file, you need to turn of caching like so :

$.ajax({
   url   : 'whatever.html',
   type  : 'POST',
   cache : false
})
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thanks. Do you know if it is possible for server to serve with 304 before a successful 200 response. – user462455 Nov 19 '13 at 18:32
  • No it is not, your header can only contain one response code per request, so it's either or. You can turn of caching for that file on the serverside, but how to do that depends on the server you're using and how it's set up ? – adeneo Nov 19 '13 at 18:35
  • I think I was not clear. I meant can a server respond with 304 code before it responded with a 200 response code. Suppose client starts downloading the file but ends it in between. What would response code be for that killed request. And what would be response code for subsequent requests – user462455 Nov 19 '13 at 18:52
  • That's not how it works, the browser sends a request to the server asking for a resource, the server sends a header indicating what to do, if it's 200, it's okay, that file exists and we can start a download, it it's 304 that file is already cached in the browser and we don't have to download it again, if it's 404 that file doesn't exist at all. Now, you can send one response code and then change it, as the headers are already sent at that point, you can only send one response code, so it's either or, you can't do both. The answer above should solve it with ajax requests anyway. – adeneo Nov 19 '13 at 18:57