Jason Krs Jason Krs - 5 months ago 13
jQuery Question

Can't read json_encode

I'm having an issue. I have a json_encode array in php. With ajax, I get back in my javascript script.

php

<?php
$code = 'xyz';
$email = 'xyz@gmail.com';
$back = array();
array_push($back, array("code" => $code,"email" => $email));
echo json_encode($back);
?>


ajax callback function

function(data){
alert(data);
alert(data[0].code);
}


When I try to alert data, I get
[{"code":"xyz","email":"xyz@gmail.com"}]


Now when I try to alert the code (or the email) it says
undefined
.

Can you help me alert data[0].code properly ?

Answer

In your ajax options set dataType: "JSON"