Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

header('Content-type: text/json');
header('Content-type: application/json');

which one should be used ?

share|improve this question
2  
possible duplicate of The right JSON content type? – John Nov 2 '11 at 9:40
70  
How can a question, asked 2008, be a duplicate of a question, asked an year later? – Дамян Станчев Jul 12 '12 at 5:14
5  
Time travel! haha – Ignas Feb 8 at 11:39

7 Answers

up vote 330 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
1  
I see. I wonder what would happen if you use the wrong Content-type ? – andyk Nov 6 '08 at 3:25
1  
@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
@S.Lott: I see, thanks for the info. – andyk Nov 7 '08 at 4:03
4  
FWIW: Drupal uses "Content-Type: text/javascript; charset=utf-8" (see file 'includes/common.inc') – bart Feb 4 '11 at 9:00

For JSON:

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

For JSON-P:

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

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

share|improve this answer
1  
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
6  
+1 for having the code to easily copy/paste :) – Steve-O-Rama May 2 at 18:41

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

share|improve this answer
thanks. That's good to know. – andyk Nov 6 '08 at 3:32

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
1  
@Matthew there's a few Firefox plugins that will render it instead too, e.g. JSONView – Rup Jan 10 '12 at 16:02
1  
Firefox 19 renders JSON as text with no problem. Don't use text/html. – m93a Mar 7 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 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 at 8:36

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.

I.e.

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
15  
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 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 at 3:04

1) JSON :

Response is Dynamically generated data, according to the Query Parameters passed in the URL.

For Eg : { "Name": "Foo", "Id": 1234, "Rank": 7 }

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

2) JSON-P :

Response is JSON data, with a function call wrapped around it.

For Eg : functionCall({"Name": "Foo", "Id": 1234, "Rank": 7});

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

share|improve this answer

Even we can do like below:

<?php
    header('Content-Type: text/javascript; charset=utf8');
    header('Access-Control-Allow-Origin: http://www.example.com/');
    header('Access-Control-Max-Age: 3628800');
    header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');

    $file='rss.xml';
    $arr = simplexml_load_file($file);//this creates an object from the xml file
    $json= '('.json_encode($arr).');'; //must wrap in parens and end with semicolon
    print_r($_GET['callback'].$json); //callback is prepended for json-p
?>
share|improve this answer
2  
So when an application reads json has to have text/javascript headers? No! This is wrong. Also the warping is wrong for standar json. – Jimmy Kane May 3 at 14:25

protected by Community Sep 13 '11 at 2:49

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

Not the answer you're looking for? Browse other questions tagged or ask your own question.