How to access data in JSON response recieved from JQuery?
This question already has an answer here:
If you add dataType: "json"
to the call the response will be made a json object:
$.ajax({
url: 'buildings.php',
data: "building=" + building,
dataType: "json",
complete: function (response) {
alert(response.name);
}
});
Edit: so it appears that for whatever reason jQuery wasn't able to parse it automatically, but JSON.parse(response.responseText)
did the trick.
你可以jQuery.getJSON()
并检查响应的contentType
Is your PHP script returning the correct MIME type in the headers? As shown here - Returning JSON from a PHP Script
If so, then add this to the options.
dataType: "json",
One of the easiest mistakes to make if your content header is right is that of returning a quoted string instead of the actual JSON. ie. the actual return contents being
"{ "key": "value" }"
instead of
{ "key": "value" }
链接地址: http://www.djcxy.com/p/8496.html