3

I am working on a web service the needs to return JSON data. I read that I should use application/json but am not sure what problems this may cause?

For example, will older browsers support it? (IE6+, FF, Opera, etc.)

Or is it possible that users behind corporate firewalls/proxy servers block the mime type application/json?

What, if any, problems have you had following this advice?

Community
  • 1
  • 1
Dai Bok
  • 3,065
  • 2
  • 46
  • 60

3 Answers3

3

Let's consider IE. Say you have a hidden iFrame which you use to request a file download. For example

<iframe src="getFile?id=123">

Now, the server may send a JSON-encoded error message like

{
    error: 'File 123 does not exist',
    retryLater: false
}

If that error message is sent as application/json, a download dialog will appear, because the JSON text is mistaken for the actual file.

On the other hand, a MIME type of text/plain will cause the message to be rendered in the iFrame, and you can extract it, and transform it into a fancy pop-up or something using JScript.


(Edit)

Real-world example: EXTJS Fileupload - Problem with IE8 security bar

Community
  • 1
  • 1
user123444555621
  • 130,762
  • 25
  • 104
  • 122
  • Thanks pumbaa80, and also considering what Eric said below, it looks like using text/x-json may be best. – Dai Bok Jan 30 '11 at 21:19
  • I only realized just now that `text/x-json` brings up a download dialog too, so you need `text/plain` for the iframe contents. Also, [uploading](http://yuilibrary.com/projects/yui2/ticket/2528941) is a much more useful example than downloading. – user123444555621 Mar 02 '11 at 23:51
1

Having just had a long fight with IE8 myself with this I found that if you're loading the json into an iframe as text/plain, IE8 will wrap this in a tag. If you then fetch the content out with innerHTML and try to parse this as json, it'll fail.

I ended up having to send the content as text/html, which just seems totally wrong, but works in IE and doesn't seem to mess up other browsers more modern AJAX handling.

Norgg
  • 181
  • 1
  • 6
1

This has been discussed before:

What is the correct JSON content type?

Any firewalls that block the MIME type will cause problems with any AJAX-style web apps, so I really wouldn't worry about it.

Community
  • 1
  • 1
Eric Giguere
  • 3,435
  • 13
  • 11
  • Thanks for your comment, Yes I read the dicussion you pointed out. I was looking for more real world concrete examples, that some SO user may have experiences. Like Pumbaa80 answer below. Good to know about firewalls. Thanks – Dai Bok Jan 26 '11 at 09:32