我试图显示我得到的json并解析它在ajax的成功函数中。解析json使用ajax成功函数
我到目前为止有:
阿贾克斯:
data = "Title=" + $("#Title").val() + "&geography=" + $("#geography").val();
alert(data);
url= "/portal/getResults.php";
$.ajax({
url: url,
type: "POST",
//pass the data
data: data,
dataType: 'json',
cache: false,
//success
success: function(data) {
alert(data);
}
});
getResults.php(JSON输出):
{
"results": [
{
"DocId": 2204,
"Title": "Lorem ipsum dolor sit amet, consectetur",
"Locations": [
{
"State": "New York",
"City": ""
},
{
"State": "New York",
"City": "New York City"
}
],
"Topics": [
3,
7,
11
],
"PublicationYear": "2011",
"Organization": "New Yorks Times",
"WebLocation": "www.google.com",
"Description": "Lorem Ipsum"
}
],
"TotalMatches": 1
}
我希望得到的结果数据是从json的getResults.php,但是我得到[object Object]。
我也曾尝试下面的代码,但没有得到响应:
success: function(data) {
var json1 = JSON.parse(data);
alert(json1);
}
尝试'JSON.stringify(data)' – depperm
您收到一个对象。当使用alert()显示时,它将被转换为一个字符串,默认为'[object Object]'。试试'console.log()'来看看实际的对象。 – Sirko
jQuery已经为你解密了数据,你不需要使用'JSON.parse'。你看到'[object Object]'的原因是因为你使用了'alert()'来查看它 - 这会强制所有类型都是字符串。改用'console.log' –