Take the 2-minute tour ×

This question already has an answer here:

I've searched around and found two different ways to define Content-type for JSON file loaded with PHP.


Which one should be used?

share|improve this question
add comment

marked as duplicate by George Stocker Oct 4 '13 at 15:24


7 Answers

up vote 449 down vote accepted

AFIK IANA has officially registered a MIME type for JSON as application/json in RFC4627, also is listed in the Internet Media Type list.

share|improve this answer
I see. I wonder what would happen if you use the wrong Content-type ? –  andyk Nov 6 '08 at 3:25
@andyk: Depends on your client. For our web services, for example, our client library will accept non-standard text/json and x-text/json and a few others. –  S.Lott Nov 6 '08 at 11:06
FWIW: Drupal uses "Content-Type: text/javascript; charset=utf-8" (see file 'includes/common.inc') –  bart Feb 4 '11 at 9:00
add comment





As @EricCope mentioned, you probably might also want to append ; charset=utf-8 if you're outputting UTF-8 data.

share|improve this answer
I was having issues with Firefox throwing an error when I was using header('Content-Type: application/json'); I had to use: header("Content-Type: text/javascript; charset=utf-8"); –  Eric Cope Oct 2 '12 at 4:35
add comment

Look at things like the Apache Axis spec. They favor application/json.

share|improve this answer
add comment

application/json is favored but usually web browsers don't know what to do with those headers and screws it up. For stuff like jquery, I have seen text/html recommended. If you are having errors pop up (e.g. download dialog box) then try text/html

share|improve this answer
Chrome is the only browser on my machine that displays json in the browser rather than offering to save it with a dialog –  Matthew Lock Oct 26 '11 at 8:37
@Matthew there's a few Firefox plugins that will render it instead too, e.g. JSONView –  Rup Jan 10 '12 at 16:02
Firefox 19 renders JSON as text with no problem. Don't use text/html. –  m93a Mar 7 '13 at 19:31
No it doesn't matter how the browser behaves. We are talking about headers that also extent to an application support and usage. This is totally wrong. –  Jimmy Kane May 3 '13 at 14:22
I agree that the text/html header is wrong, but to those of us who write applications that run in a browser, how the browser behaves is by its definition not only important, but critical to how our applications function. –  toon81 Jun 14 '13 at 8:36
add comment

I recently had a very strange run in with this.

I had a form which used jQuery to do an AJAX lookup against a PHP script, and then return the response as JSON, formed using PHP's json_encode() function.

It was all working fine for a couple of months, then this morning it stopped working... It turns out the javascript couldn't parse the JSON correctly.

My original method used eval.


var p = eval('(' + msg + ')');

But that gave me the error:

missing ] after element list

I then tried using

var p  = JSON.parse(msg);

Using the json2.js library from http://json.org. This also failed.

I checked my JSON was formatted correctly using an online tool - it was.

In the end I tried changing the header type I was setting in the PHP script which was being called by jQuery AJAX.

I changed it from application/json to text/plain

And both the eval() and the JSON.parse solutions immediately worked... Much to my relief as I was running out of ideas.

Very odd and still don't know quite why, but thought I'd post here in case someone runs into a similar issue, or anybody can provide an explanation.

share|improve this answer
In Jquery 1.3, you used to need to eval things. Starting with jquery 1.4, if it's application/json data returned to a jquery ajax call, jquery will parse it for you (using the safer json parse approach). This is why your parsing was failing - it was already parsed by jquery for you. Once you changed it to text/plain jquery quit parsing it for you because it didn't know that it was getting back json data. –  David Eison Sep 6 '11 at 18:43
@David Eison, I having the same issue as Tom. My code is this: $.ajax({ url: 'get_cpu.php', success: function(data) { alert(data); } }); I get undefined from that alert. It works perfectly on chrome/safari/firefox. Any ideas what I might need to do? –  user1471980 Jun 14 '13 at 16:07
@user1471980: Probably not same issue as Tom's, his issue was caused by explicitly parsing already parsed data. Your issue, too little info to diagnose, but make sure that get_cpu.php is actually returning some json data, e.g. run through a web proxy like fiddler and see what you're sending back. Open up a new question with detailed info if still stuck. –  David Eison Jun 20 '13 at 3:04
add comment




Answer : header('Content-Type: application/json');




Answer : header('Content-Type: application/javascript');

share|improve this answer
add comment

The first two headers prevent the browser from caching the response (a problem with IE and GET requests) and the third sets the correct MIME type for JSON.

share|improve this answer
You don't seem to have read the question at all here. There are only two headers in the question, and neither of them has anything at all to do with caching. –  Andrew Barber Aug 26 '13 at 22:58
The answer is still relevant because they are associated with JSON requests and the problems with caching –  b.kelley Sep 25 '13 at 16:53
add comment

protected by Community Sep 13 '11 at 2:49

