Reputation: 3
I have this encode in JSON format as returned data:
{
"error": {
"msg":"Concurrent verifications to the same number are not allowed",
"code":10
}
}
and I want to access msg so I wrote the javascript as:
$("#buttonPhoneSubmit").click(function(e) {
$("#verifyForm").show();
e.preventDefault();
$.ajax({
type: 'post',
url: './process.php',
data: $('form').serialize(),
datatype:'json',
success: function (data) {
$('#error_message').html(data.error.msg);
}
});
but it said the data is undefined. Can someone tell me what's wrong with the code?
Thanks!
Upvotes: 0
Views: 62
Reputation: 1074098
As Roy said, you have datatype: 'json'
instead of dataType: 'json'
. So I suspect jQuery isn't parsing the JSON for you.
While you could change it to dataType: 'json'
instead, the better approach is to update the PHP file to send the Content-Type: application/json
header with the response:
// In the PHP, prior to sending the body of the response
header('Content-Type: application/json');
...and remove datatype: 'json'
from your ajax
call. jQuery will see the content type and parse it for you, at which point your code should work (assuming the page return returns the JSON you've quoted).
Upvotes: 2