I am banging my head against the wall here, and I am hoping someone can help me out.
I have an AJAX function which calls a PHP page. That page returns a JSON object, which should then be parsed and displayed to the user. Everything works fine except when the JSON object is returned, trying to parse it gives undefined.
The PHP:
$jsonArray= array(
'request' => 'this is the request',
'response' => 'this is the response'
);
echo json_encode($jsonArray);
On the Ajax side, I do the following:
var display=xmlHttp.responseText;
alert(display); //gives {"request":"this is the request","response":"This is the response"}
alert(display.request); //gives undefined
Am I missing something obvious? Pasting the same string directly into a JavaScript variable seems to work fine...
application/json
. You can use the PHP codeheader('Content-type: application/json');
before the echo statement to set this. – PleaseStand Nov 6 '10 at 6:25