The Application/json and Text/javascript of Content-type

Source: Internet
Author: User

We typically use the following code when the server returns JSON-formatted data to the browser:

response.setcontenttype ("Text/javascript; charset=utf-8");

Or:

Response.setcontenttype ("Application/json;charset=utf-8");

But what is the difference between these two?

It's actually very simple. The former is used to return the JS code, which is specifically used to return data in JSON format.

That is, the JS code returned by the former is automatically executed in the browser, just as the returned result is called by eval (result). That is, the returned string, will be treated as JS code, by the browser's JS engine execution. Instead of being displayed as a normal string in the browser.

The latter is designed to return data in JSON format, which is a subset of the JS code. In other words, JSON-formatted data, as well as JS code, will be executed by the browser's JS engine, and the JSON object is generated.

However, the JSON format is much smaller than the range of JS code. JSON is a subset of JS. For specific format requirements for JSON, see: http://www.json.org/

So if the JSON format is returned, then both are possible.

------------------------------

When the server sends JSON data to the client:
Content-type = ' Application/json;charset=utf-8 '

When the server sends the JS code to the client:

Content-type = ' Text/javascript;charset=utf-8 '

When the server determines whether the client is submitting JSON data:

Content-type = ' Application/json;charset=utf-8 '
Content-type = ' Text/json;charset=utf-8 '
Content-type = ' Text/javascript;charset=utf-8 '
Content-type = ' Application/javascript;charset=utf-8 '

As long as Content-type satisfies any of the above 4 conditions, it is considered that the submitted data is JSON data

The Application/json and Text/javascript of Content-type

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.