Content consolidation is ignored when using a browser.

Here's the situation:

I have a web application, a response to a request for a list of resources, say:

/items 

Initially, the request is requested directly by the web browser, moving along this path. The browser uses the standard "Accept" header, which includes "text / html", and my application notices this and returns the HTML content for the list of elements.

The returned HTML has some JavaScript (jQuery) that then executes an ajax request to retrieve the actual data:

 /items 

Only this time, the Accept header is explicitly set to application / json. Again, my application notices this, and JSON returns correctly to the request, the data is inserted into the page, and everyone is happy.

The problem comes . The user goes to another page and then presses the BACK button. They are then prompted to save the file. This, as it turned out, is the JSON data of the item list.

So far, I have confirmed that this will happen in both Google Chrome and Firefox 3.5.

There are two possible types of answers:

  • How can I fix the problem. Is there some kind of magic combination of Caching Headers or other voodoo that make the browser do the right thing here?

  • If you think I'm doing something terribly wrong here, how do I go about it? I am looking for correctness, but also trying not to sacrifice flexibility.

If this helps, the application is a JAX-RS web application using Restlet 2.0m4. I can provide sample request / response headers if this is useful, but I find the problem fully reproducible.

+6
json ajax web-applications jax-rs content-negotiation
source share
3 answers

Is there some kind of magical combination of Cache-Control or other voodoo headers that make the browser do the right thing here?

If you are submitting different answers to different Accept: headers, you must include the header:

 Vary: Accept 

in your answer. The Vary header should also contain any other request headers that affect the response, for example, if you are doing gzip / deflate compression, you need to enable Accept-Encoding.

IE, unfortunately, does poorly with many Vary values, completely breaking caching, which may or may not matter to you.

If you think that I am doing something terribly wrong, how should I do it?

I don’t think the idea of ​​serving different content for different types at the same URL is terribly wrong, but you allow yourself more compatibility issues than you really need. Relying on headers working through JSON is not really a good idea; your best bet is to have a different url like /items/json or /items?format=json .

+6
source share

I know this question is old, but just in case someone comes across this:

I had the same problem with a Rails application using jQuery, and I fixed it by telling the browser not to cache the JSON response with the solution given here to another question:

jQuery $ .getJSON only works once for each control. Does not reach the server

The problem only arose with Chrome and Firefox. Safari completely coped with the reverse behavior, clearly not specifying that it did not cache.

+1
source share

An old question, but for those who see this, there is nothing wrong with using the Accept header in the question.

This is a confirmed bug in Chrome. (Formerly also in Firefox, but with a fix.)

http://code.google.com/p/chromium/issues/detail?id=94369

0
source share

All Articles