Reputation: 589
I feel as if I'm making a noob mistake here, but it's annoying the hell out of me so I guess I'll post!
I'm trying to get message
in the php array to be logged to the console. What actually happens is, data returns nothing, message returns 'success' and data.message is undefined.
JavaScript:
var message = '';
jQuery.ajax({
url: '/dir/myfile.php',
type: "POST",
dataType: "json",
data: { message : message },
error: function(xhr, text, thrown){
console.log('error');
},
success: function(data, message, text, xhr){
if(data.message){
console.log('data exists');
} else if(message){
console.log(message);
}else{
console.log('no data');
}
},
})
PHP:
$output = array(
'code' => 1,
'message' => 'Please work!'
);
echo json_encode($output);
Upvotes: 0
Views: 128
Reputation: 37706
According to https://api.jquery.com/jQuery.ajax/, the prototype of the success callback should be :
Function(PlainObject data, String textStatus, jqXHR jqXHR)
Anyway, you should not do a test like this :
if (data) { ... } else if (msg) { ... }
Because your message is your data !
What you want looks like that :
if (data) { console.log(data.code) ; console.log(data.message) ; }
Upvotes: 2