121

I have a webservice that when called without specifying a callback will return a JSON string using application/json as the content type.

When a callback is specified it will wrap the JSON string in a callback function, so it's not really valid JSON anymore. My question is, should I serve it as application/javascript in this case or still use application/json?

Zach
  • 24,496
  • 9
  • 43
  • 50

2 Answers2

147

Use application/javascript. In that way, clients can rely on the content-type without having to manually check whether a response has padding or not.

apaderno
  • 28,547
  • 16
  • 75
  • 90
John Millikin
  • 197,344
  • 39
  • 212
  • 226
  • 1
    It fails in IE 9 with message "SEC7112: Script from http://xyz.com was blocked due to mime type mismatch " Any idea why ? – Pit Digger Sep 12 '11 at 20:05
  • Content type "application/javascript" may mismatch with "text/javascript" that specified in script tag (or expected by default for some reason) – Ievgen Lukash Mar 26 '13 at 21:18
  • Based on the information in [this article](http://blogs.msdn.com/b/ie/archive/2010/10/26/mime-handling-changes-in-internet-explorer.aspx) text/javascript and application/javascript would work in IE 9. I wonder @pit-digger if the server just wasn't returning the correct content-type header. – spig May 06 '14 at 14:13
122

Use application/json as per rfc4627.txt if what you return is plain JSON.

If you return JavaScript (which is really what JSONP is), then use application/javascript as per rfc4329.txt

Brad
  • 159,648
  • 54
  • 349
  • 530
Florian Bösch
  • 27,420
  • 11
  • 48
  • 53