Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the correct json headers?

Is there a way in htaccess to ensure the headers for my json are correct?

Update: Does anyone see anything wrong with these headers for json?

Date    Mon, 26 Jul 2010 08:31:11 GMT
Server  Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.7a mod_fcgid/2.3.5 Phusion_Passenger/2.2.15 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By    PHP/5.2.13
X-Pingback  http://brettbarros.com/wordpress/xmlrpc.php
Content-Disposition attachment; filename="json_api.json"
Vary    Accept-Encoding
Content-Encoding    gzip
Content-Length  719
Keep-Alive  timeout=5, max=98
Connection  Keep-Alive
Content-Type    application/json; charset=UTF-8

Specifically, it's working with jquery's getJSON in ie8, ffx, chrome, but not ie7 or ie6...

like image 436
Matrym Avatar asked Jul 26 '10 08:07

Matrym


People also ask

What is the correct JSON Content-Type?

JSON has to be correctly interpreted by the browser to be used appropriately. text/plain was typically used for JSON, but according to IANA, the official MIME type for JSON is application/json .

What is header in JSON format?

The header is there so your app can detect what data was returned and how it should handle it. You need to look at the header, and if it's application/json then parse it as JSON. This is actually how jQuery works. If you don't tell it what to do with the result, it uses the Content-Type to detect what to do with it.

What is application JSON format?

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

How do you set the Content-Type of the response to JSON?

The correct MIME media type for JSON is application/json . JSP will use it for sending a response to the client. Show activity on this post. “ application/json ” is the correct JSON content type.


2 Answers

AddType application/json .json

is a simple way to make all your *.json files being sent with the correct mime type. That, of course, doesn't work, if you create them dynamically in something like a, say, PHP script. In that case, you can add the info inside the script:

<?php
header('Content-Type: application/json');
// ...
like image 127
Boldewyn Avatar answered Nov 12 '22 09:11

Boldewyn


You can inspect the headers sent along from the server side using Firebug's Net tab. It shows all the headers for both the request and the response.

like image 42
Roman Avatar answered Nov 12 '22 10:11

Roman