javascript - Cannot parse json data from ajax call. Attempted fixes cause me to go in circles. Why? -
update: @messiah told me shorten this. here bare bones version.
i cannot parse response ajax call. keep going in circles. google telling me this problem. well, no matter if take out json in ajax call or call parse. give me errors.
if parse directly without using json.parse(), get: first 1 is: uncaught typeerror: cannot read property 'time' of undefined
okay, means not parsed.
currently, getting because trying parse: 1 getting right is: uncaught syntaxerror: unexpected token u
okay, means parsed.
here ajax call:
var reply; function sendrouterequest() { $.ajax({ type: 'get', //contenttype: "application/json; charset=utf-8", url: '/api/getreply', data: data, datatype: 'json', success: function(result) { reply = result; console.log(reply); console.log(reply.time); }, error: function(jqxhr, textstatus, errorthrown) { alert("error: " + textstatus + " exception: " + errorthrown); } });//end of ajax }
here relevant code:
function parsejson() { //data = json.stringify(reply); jsondata = json.parse(reply); console.log(jsondata.time); }
here reply looks in console.
object alert: object points: array[30] points: array[30] time: 4.72 __proto__: object status: object __proto__: object
but console.log(reply.time) gives me undefined. wut?
going in circles.
geeky
update: calling parsejson() in function not shown here, $(document).ready(function(){ //blah blah blah}
servlet returns application/json format
this doesn't response. eg why there 2 points.
points: array[30] points: array[30]
can print out response get. looks javascript object , not json response.
if json response should
[{"alerts" : {}, "points": [], "time": 4.72 ......}]
i think uncomment line
datatype: 'json',
Comments
Post a Comment