Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is 'jQuery.parseJSON' not necessary?

i'm doing an ajax request with query and wondering why my response is already a JS object.

If i do a

var obj = jQuery.parseJSON(response);

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

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

Thanks

like image 877
Sebastian Avatar asked Dec 01 '22 00:12

Sebastian


2 Answers

This happens when you make an AJAX call and specify the dataType JSON jQuery calls jQuery.parseJSON on the response for you. In fact you can specify what function to call depending on the dataType as you can se from the documentation

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

So if you make a call like this

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

If you don't specify a dataType jQuery tries to guess it

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

The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:

"xml": Returns a XML document that can be processed via jQuery.
"html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM. "script": Evaluates the response as JavaScript and returns it as plain text. Disables caching by appending a query string parameter, "=[TIMESTAMP]", to the URL unless the cache option is set to true. Note: This will turn POSTs into GETs for remote-domain requests. "json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)
"jsonp": Loads in a JSON block using JSONP. Adds an extra "?callback=?" to the end of your URL to specify the callback. Disables caching by appending a query string parameter, "
=[TIMESTAMP]", to the URL unless the cache option is set to true.
"text": A plain text string. multiple, space-separated values:
As of jQuery 1.5, jQuery can convert a dataType from what it received in the Content-Type header to what you require. For example, if you want a text response to be treated as XML, use "text xml" for the dataType. You can also make a JSONP request, have it received as text, and interpreted by jQuery as XML: "jsonp text xml." Similarly, a shorthand string such as "jsonp xml" will first attempt to convert from jsonp to xml, and, failing that, convert from jsonp to text, and then from text to xml.

like image 200
Nicola Peluchetti Avatar answered Dec 04 '22 10:12

Nicola Peluchetti


It pretty much depends which dataType you pass into your jQuery ajax request. This might happen implict by calling .getJSON() or directly using $.ajax().

However, if you omit the dataType, jQuery trys to do some magic and guesses which data was received. As for JSON data, it uses a simple regular expression to check if a response looks like a JSON-string and if so, it automatically parses it for you. jQuery will try to infer it based on the MIME type of the response.

So always be precise and tell jQuery which type of data you expect.

like image 43
jAndy Avatar answered Dec 04 '22 09:12

jAndy