what is the use of $.toJSON() function. it seems that it convert the data in json format.
here is the code snippet
data: $.toJSON({ name: $("input[type=text]").val() })
without using $.toJSON() function we can supply data manually in josn format like
data: { name: $("input[type=text]").val() }
it will also work i think. please discuss. thanks
First,
toJSONis not a native jQuery method.Assuming it does the same as
JSON.stringify, it is not the same as assigning an object to thedataoption. From the documentation (emphasis mine):So if you don’t pass a string (i.e. the object), the data is converted to a query string, not JSON.
To be more precise
would result in this URL (assuming GET (
%22is")):This would make the value difficult to access I suppose. It makes more sense using this ina POST request.
On the other hand,
results in
Maybe important to point out is that in your example
is not “JSON format”. It is a JavaScript object literal. JSON is a data exchange format, and although its syntax is similar to JavaScript’s object literals, it is something completely different.