In my javascript, I have a jQuery ajax get request:
$.get(uri, callback_function, 'json');
In firebug, I can see the response header:
Content-Type application/json
the response content:
{ "status": true, "data": "my test output" }
There is even a JSON tab in Net -> XHR which shows the returned data pretty-printed.
However, why in my callback function do I need to parse the data I get?
function callback_function(data) {
console.log(data); // { "status": true, "data": "my test output" }
// (printed in black)
console.log(data.status); // undefined
var parsedData = JSON.parse(data); // why is this needed??
console.log(parsedData); // Object { status=true, data="my test output"}
// (printed in colours)
console.log(parsedData.status); // true
}
From the documentation:
https://api.jquery.com/jQuery.get/
jQuery.get( url [, data ] [, success ] [, dataType ] )
dataType Type: String
The type of data expected from the server. Default: Intelligent Guess (xml, json, script, text, html).
Since I am both expecting and getting a JSON object (see HTTP response header), why do I need to parse what appears to be a mere string?