Why is "jQuery.parseJSON" not needed?

I make an ajax request with the request and wonder why my answer is already a JS object.

If i do

var obj = jQuery.parseJSON(response); 

'obj' is null, but I can use 'response' as an array of js objects.

This is not a problem, but I would like to understand this behavior.

thanks

+4
source share
5 answers

This happens when you make an AJAX call and specify jQuery jQuery jQuery jQuery dataType to answer it. In fact, you can specify which function to call depending on the data type, as you can from the documentation

Converters

(added 1.5)
Map Default: {"* text": window.String, "text html": true, "text json": jQuery.parseJSON, "text xml": jQuery.parseXML} Map of dataType-to-dataType converters. each converter value is a function that returns the converted value response

So if you make such a call

 $.ajax({ url: yoururl, dataType: "json", success: function(data){ //data is already a json } 

If you did not specify dataType, jQuery tries to guess it

dataTypeString Default: Intelligent Guess (xml, json, script or html)

The type of data you expect from the server. If none, jQuery will try to derive it based on the MIME type of the response (the XML MIME type will give XML, in 1.4 JSON there will be a JavaScript object, in 1.4 the script will execute the script, and everything else will be returned as a string). Available types (and the result obtained as the first argument to your success callback):

"xml": returns an XML document that can be processed through jQuery.
"html": returns HTML as plain text; script tags are included when pasting into the DOM. "script": evaluates the response as JavaScript and returns it as plain text. Disables caching by adding a query string parameter, "= [TIMESTAMP]", to the URL if the option cache is set to true. Note. This will turn POST into GET for remote domain requests. "json": evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4, JSON data is parsed in strict order; any rejected JSON is rejected and the parsing error is thrown. (See Json.org for more information on proper JSON formatting.)
"jsonp": loads into a JSON block using JSONP. Adds an extra "? Reverse =?" to the end of your url to specify a callback. Forbids caching by adding a query string parameter, "= [TIMESTAMP]", to the URL if the cache parameter is set to true.
"text": plain text string. multiple values โ€‹โ€‹separated by spaces:
Starting with jQuery 1.5, jQuery can convert the data type from the value you need in the Content-Type header. For example, if you want the text response to be processed as XML, use "text xml" for dataType. You can also do JSONP if it received text and interpreted jQuery as XML: "jsonp text xml". Similarly, a string string such as "jsonp xml" will first try to convert from jsonp to xml, and, what, convert from jsonp to text, and then from text to xml.

+10
source

dataType much depends on which dataType you are passing into your jQuery ajax request. This can happen when calling .getJSON() or directly using $.ajax() .

However, if you omit dataType , jQuery tries to do some magic and guesswork whose data was received. Regarding JSON data, it uses a simple regular expression to check if the answer looks like a JSON string, and if so, it will automatically parse it for you. jQuery will try to infer it based on the MIME type of the response.

Therefore, always be precise and tell jQuery what type of data you expect.

+4
source

The default behavior of jQuery ajax is to parse the response and return it as the most appropriate data type. If your answer looks like JSON, so it will be converted to a JavaScript object / array.

You can override this behavior by setting the dataType attribute in ajax settings.

+2
source

if you specify dataType as json , jquery parses the answer for you as

 $.ajax({ ... dataType:'json', ... }); 

the same thing happens with jQuery.getJSON ()

This is the source code for getJSON

 getJSON: function( url, data, callback ) { return jQuery.get( url, data, callback, "json" ); }, 

https://github.com/jquery/jquery/blob/master/src/ajax.js#L283

+2
source

Because the

 jQuery.ajaxSettings.converters["text json"] === jQuery.parseJSON 

IE, it will run the function every time a json response is automatically detected or explicitly set independently

+1
source

Source: https://habr.com/ru/post/1394372/


All Articles