javascript - AJAX and JS, Cannot read JSON data? -
i have following ajax code:
var ajax = new xmlhttprequest(); axaj.open("post", "index.php", true); ajax.setrequestheader("content-type", "application/x-www-form-urlencoded"); ajax.onreadystatechange = function(){ if(x.readystate == 4 && x.status == 200){ var returnval = ajax.responsetext; } } ajax.send("nextmax=-1");
and pairs php ends with:
echo json_encode(array( 'next_id' => $nextid )); exit();
this works, is. if print out returnval
inside ajax call, prints out correct array, correct value:
{"next_id":"935210077606657948"}
but cannot access id directly. i've tried
var nextid = returnval.next_id;
and
var nextid = returnval['next_id'];
and other variations, return undefined
.
how array elements within returnval
?
thanks in advance.
found solution not 30 seconds after posting question. in same place:
switch
var returnval = ajax.responsetext;
to
var returnval = json.parse(ajax.responsetext);
and call works:
returnval.next_id;