从JQuery.ajax成功数据解析JSON [英] Parse JSON from JQuery.ajax success data
本文介绍了从JQuery.ajax成功数据解析JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我无法从JQery.ajax调用获取JSON对象的内容.我的电话:
I am having trouble getting the contents of JSON object from a JQery.ajax call. My call:
$('#Search').click(function () {
var query = $('#query').valueOf();
$.ajax({
url: '/Products/Search',
type: "POST",
data: query,
dataType: 'application/json; charset=utf-8',
success: function (data) {
alert(data);
for (var x = 0; x < data.length; x++) {
content = data[x].Id;
content += "<br>";
content += data[x].Name;
content += "<br>";
$(content).appendTo("#ProductList");
// updateListing(data[x]);
}
}
});
});
似乎正确返回了JSON对象,因为"alert(data)"显示以下
It seems that the JSON object is being returned correctly because "alert(data)" displays the following
[{"Id": "1", "Name": "Shirt"}, {"Id": "2", "Name":"Pants"}]
但是当我尝试使用以下方式在页面上显示ID或名称时:
but when I try displaying the Id or Name to the page using:
content = data[x].Id;
content += "<br>";
content += data[x].Name;
content += "<br>";
它将未定义"返回到页面.我在做什么错了?
it returns "undefined" to the page. What am I doing wrong?
感谢您的帮助.
推荐答案
数据以JSON的字符串表示形式返回,您无需将其转换回JavaScript对象.将dataType
设置为'json'
使其自动转换.
The data is coming back as the string representation of the JSON and you aren't converting it back to a JavaScript object. Set the dataType
to just 'json'
to have it converted automatically.
这篇关于从JQuery.ajax成功数据解析JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文