14

For one of our ajax request (with a .json response) some of our clients have complained that they are seeing a "File Download" prompt asking the user to download the .json response. I am baffled because considering that this is an xhr response, this should never happen. Has anyone seen this?

Thanks

  • why you add .json as file extensions ? i think you cannot do that unless you set the browser to do specific thing for .json file (in FF go to options) – nightingale2k1 Jul 02 '09 at 05:09

5 Answers5

16

For people who are using ASP MVC and have the same problem with IE, use this when returning your response:

return Json(result, "text/plain");

Edit: the standard type is: "application/json", but does not work with IE. using text/html is dangerous.

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
Reza
  • 834
  • 1
  • 7
  • 18
  • Better answer than @jrharshath, because text/plain is not technically correct. It's a gross hack for old versions of IE. – Mark E. Haase Aug 24 '12 at 14:08
11

try specifying a MIME type of "text/plain" in the response. or just drop the ".json" extension from the url (try .txt, or .js, for instance)

jrharshath
  • 25,975
  • 33
  • 97
  • 127
  • and make sure before specifying this that you aren't setting the headers anywhere else in your code (ie, specifying the mime type as something other than text/plain). – jellyfishtree Nov 02 '10 at 04:03
  • This answer assumes that you have control over the response. How about calling a third-party WS that responds with JSON, with a content type set to, god forbids, json ? – phtrivier Dec 13 '11 at 15:14
  • Yes, changing ContentType to text/html (from application/json) worked for me. I wrote a blog post about it with some more details: http://blog.degree.no/2012/09/jquery-json-ie8ie9-treats-response-as-downloadable-file/ – Andreas Oct 09 '12 at 09:32
3

Not sure if you found a solution, but I had a similar problem where IE tried to download any JS responses. To fix it, I had to ensure that format.html appears above format.js in the response block:

def index

  # ...

  respond_to do |format|
    # html must be above js, otherwise IE will try to download the JS
    format.html
    format.js
  end
end

Hope this helps.

Chris Blunt
  • 986
  • 9
  • 23
  • This solution worked for me. No idea why (other browsers worked fine). – Kyle Fox May 26 '10 at 20:41
  • IE Accept header: `Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, */*` Firefox Accept header: `Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8` Not sure what the q= means yet, but i bet it has something to do with how much it wants content of that particular type. – Peter Kovacs Oct 07 '10 at 14:53
1

I am using the QQ ajax file uploader and I found that I needed to set the content type to "text/json" for it to work properly.

gabeodess
  • 2,006
  • 21
  • 13
-3

Drop the .json and set the content type as text/html. IE doesn't know what type of file you are sending it, so it offers to download. It knows what to do with text/html :)