如何访问 jQuery.ajax() 返回数据
我使用以下代码进行 AJAX 调用:
$.ajax({
url: href,
type: 'POST',
data: {},
dataType: "json",
error: function(req, resulttype, exc)
{
// do error handling
},
success: function(data)
{
for (var tracklist in data) {
console.log(tracklist.name); // undefined
console.log(tracklist['name']); // undefined
}
}
});
我返回到 AJAX 请求的是:
{"5":{"id":5,"name":"2 tracks","count":2},"4":{"id":4,"name":"ddddd","count":1},"7":{"id":7,"name":"Final test","count":2}}
我想知道的是如何访问当前曲目列表的 name 属性。
I'm using the following code to make an AJAX call:
$.ajax({
url: href,
type: 'POST',
data: {},
dataType: "json",
error: function(req, resulttype, exc)
{
// do error handling
},
success: function(data)
{
for (var tracklist in data) {
console.log(tracklist.name); // undefined
console.log(tracklist['name']); // undefined
}
}
});
What I return to the AJAX request is:
{"5":{"id":5,"name":"2 tracks","count":2},"4":{"id":4,"name":"ddddd","count":1},"7":{"id":7,"name":"Final test","count":2}}
What I would like to know is how to access the name attribute of the current tracklist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你应该使用
而不是
You should use
instead of
如果您想迭代这些对象,最好返回一个数组:
然后,您可以使用类似于您尝试的 for 循环:
If you want to iterate over those objects, it would be better if you returned an array:
Then, you could use a for-loop similar to what you were trying:
在循环中:
tracklist
是每个元素的键,而不是它的值。因此:
In the loop:
tracklist
is the key of each element, not its value.Thus: