in javascript, have jquery ajax request:
$.get(uri, callback_function, 'json');
in firebug, can see response header:
content-type application/json
the response content:
{ "status": true, "data": "my test output" }
there json tab in net -> xhr shows returned data pretty-printed.
however, why in callback function need parse data 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 needed?? console.log(parseddata); // object { status=true, data="my test output"} // (printed in colours) console.log(parseddata.status); // true }
from documentation:
https://api.jquery.com/jquery.get/
jquery.get( url [, data ] [, success ] [, datatype ] )
datatype type: string type of data expected server. default: intelligent guess (xml, json, script, text, html).
since both expecting , getting json object (see http response header), why need parse appears mere string?
at moment string, basically, since need parse convert javascript object.
you can see in debugger pretty formatted expect debugger parsing you, since showing long string less useful.
so, parse can use result in language need, in case, in javascript.
Comments
Post a Comment