What is the correct JSON content type?

2018-12-30 23:44发布

I've been messing around with JSON for some time, just pushing it out as text and it hasn't hurt anybody (that I know of), but I'd like to start doing things properly.

I have seen so many purported "standards" for the JSON content type:

application/json
application/x-javascript
text/javascript
text/x-javascript
text/x-json

But which is correct, or best? I gather that there are security and browser support issues varying between them.

I know there's a similar question, What MIME type if JSON is being returned by a REST API?, but I'd like a slightly more targeted answer.

30条回答
谁念西风独自凉
2楼-- · 2018-12-31 00:20

If the JSON is with padding then it will be application/jsonp. If the JSON is without padding then it will be application/json.

To deal with both, it is a good practice to use: 'application/javascript' without bothering whether it is with padding or without padding.

查看更多
孤独寂梦人
3楼-- · 2018-12-31 00:21

If you are using Ubuntu or Debian and you serve .json files through Apache, you might want to serve the files with the correct content type. I am doing this primarily because I want to use the Firefox extension JSONView

The Apache module mod_mime will help to do this easily. However, with Ubuntu you need to edit the file /etc/mime.types and add the line

application/json json

Then restart Apache:

sudo service apache2 restart
查看更多
萌妹纸的霸气范
4楼-- · 2018-12-31 00:22

Not everything works for content type application/json.

If you are using Ext JS form submit to upload file, be aware that the server response is parsed by the browser to create the document for the <iframe>.

If the server is using JSON to send the return object, then the Content-Type header must be set to text/html in order to tell the browser to insert the text unchanged into the document body.

See the Ext JS 3.4.0 API documentation.

查看更多
冷夜・残月
5楼-- · 2018-12-31 00:22

Content-type: application/json - json
Content-Type: application/javascript - json-P
Content-type: application/x-javascript - javascript
Content-type: text/javascript - javascript BUT obsolete, older IE versions used to use as html attribute.
Content-type: text/x-javascript - JavaScript Media Types BUT obsolete
Content-type: text/x-json - json before application/json got officially registered.

查看更多
梦该遗忘
6楼-- · 2018-12-31 00:24

Of course, the correct MIME media type for JSON is application/json, but it's necessary to realize what type of data is expected in your application.

For example, I use Ext GWT and the server response must go as text/html but contains JSON data.

Client side, Ext GWT form listener

uploadForm.getForm().addListener(new FormListenerAdapter()
{
    @Override
    public void onActionFailed(Form form, int httpStatus, String responseText) 
    {
        MessageBox.alert("Error");
    }

    @Override
    public void onActionComplete(Form form, int httpStatus, String responseText) 
    {
        MessageBox.alert("Success");
    }
});

In case of using application/json response type, the browser suggests me to save the file.

Server side source code snippet using Spring MVC

return new AbstractUrlBasedView() 
{
    @SuppressWarnings("unchecked")
    @Override
    protected void renderMergedOutputModel(Map model, HttpServletRequest request,
                                           HttpServletResponse response) throws Exception 
    {
        response.setContentType("text/html");
        response.getWriter().write(json);
    }
};
查看更多
忆尘夕之涩
7楼-- · 2018-12-31 00:27

JSON:

Response is dynamically generated data, according to the query parameters passed in the URL.

Example:

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

Content-Type: application/json


JSON-P:

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

Example:

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

Content-Type: application/javascript

查看更多
登录 后发表回答