I want to send this $data
if($query == true)
{
$data = array(
'status'=> true,
'result' => 1
);
}
echo json_encode($data);
then I want to retrieve the data
success: function(data) {
console.log(data.status);
}
In the console is undefined
, but if I console.log(data)
, the data is called.
a) Either use JSON.parse()
success: function(data) {
data = JSON.parse(data);
console.log(data.status);
console.log(data.result);
}
b) Or in your ajax request add this only:
dataType: 'json'
You need to parse json data to use in javascript.
success: function(data) {
if ( typeof data !=="undefined" ) {
var result = JSON.parse(data);
console.log(result);
}
}
your data might still be a string, and you need to parse it to object first. You can use JSON.parse
.
let json = '{"test":"val"}';
console.log(JSON.parse(json).test); // will output val
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.