jQuery AJAX Call to PHP Script with JSON Return
Solution 1:
Make it dataType
instead of datatype
.
And add below code in php as your ajax request is expecting json and will not accept anything, but json.
header('Content-Type: application/json');
Correct Content type for JSON and JSONP
The response visible in firebug is text data. Check Content-Type
of the response header to verify, if the response is json. It should be application/json
for dataType:'json'
and text/html
for dataType:'html'
.
Solution 2:
I recommend you use:
var returnedData = JSON.parse(data);
to convert the JSON string (if it is just text) to a JavaScript object.
Solution 3:
Use parseJSON jquery method to covert string into object
var objData = jQuery.parseJSON(data);
Now you can write code
$('#result').html(objData .status +':' + objData .message);
Solution 4:
try to send content type header from server use this just before echoing
header('Content-Type: application/json');
Solution 5:
Your datatype is wrong, change datatype for dataType.