How to get one value from json_encode php to ajax?
I’m trying to get one by one of array json_encode
to jquery but it’s always getting error undefined no one work, any suggestion to my code.
$response = array(
'antrian' => true,
'message' => 'Success print recipt'
);
echo json_encode($response);
$.ajax({
url: urlPrintQueue,
method: "POST",
data: {id: id},
dataType: 'json',
success: function(result) {
console.log(result[0]); // antrian
console.log(result[1]); // message
}
});
Manda Ben
You’re running an associative array through json_encode().
If you passed a standard numeric array to json_encode(), your result in JS would be an array. When you pass an associative array, however, the result is going to be an object instead.
Instead of:
success: function(result) {
console.log(result[0]); // antrian
console.log(result[1]); // message
}
You need to use:
success: function(result) {
console.log(result.antrian); // antrian
console.log(result.message); // message
}