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