I have this ajax function:
$(document).ready(function(){
setInterval(function() {
$.ajax({
url: 'php.php',
type: 'POST',
success: function(data){
if( data != "0" ) {
alert(data.a);
}
},
});
}, 5000);
});
and a PHP to return:
<?php
header('Content-type: application/json');
...some function
if($num>0){
echo json_encode(array("a" => "valueA", "b" => "valueB"));
}
else{
echo json_encode(0);
}
?>
when I have the alert message, it shows me "UNDEFINED" instead of "valueA". Is something wrong with my array or ajax?
Set dataType: 'json'
to your $.ajax
call:
$.ajax({
url: 'php.php',
type: 'POST',
dataType: 'json',
success: ...
dataType
allows you to define type of data returned from server. By default, there's intelligent quess
and maybe that's not enough.