javascript Jquery Ajax Json 数据未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25429471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery Ajax Json data Undefined
提问by Raul Cruz
I have a URL that I have been provided with that returns data in JSON. I'm trying to display the value of each of this objects using jQuery AJAX call. I keep getting 'undefined'. What am I doing wrong?
我有一个提供给我的 URL,它以 JSON 格式返回数据。我正在尝试使用 jQuery AJAX 调用显示每个对象的值。我不断收到“未定义”。我究竟做错了什么?
The data is as follows:
数据如下:
[
[
{
"label": "First",
"value": "XXXXXX"
},
{
"label": "Second",
"value": "XXXXXX"
},
{
"label": "Third",
"value": "XXXXXX"
},
{
"label": "Fourth",
"value": "XXXXXX XXX"
},
{
"label": "Fifth",
"value": "XXXXXX"
},
{
"label": "Sixth",
"value": "XXXXXX"
}
]
]
My jQuery is as follows:
我的jQuery如下:
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'http://url',
dataType: 'json',
success: function(data) {
var items = [];
$.each(data, function() {
var label = items.label;
var value = items.value;
$('#results').append('<p>' + label + '</p>' + '<p>' + value + '</p>');
});
}
});
return false;
});
My html is:
我的 html 是:
<div id="results">
</div>
回答by user733421
You didn't add params to your each function:
您没有在每个函数中添加参数:
$.each(data, function(index,item) {
var label = item.label;
var value = item.value;
$('#results').append('<p>' + label + '</p>' + '<p>' + value + '</p>');
});
Also if your json is exaclty like you wrote it you should do $.each(data[0],function()...)
此外,如果你的 json 是像你写的那样精确,你应该做 $.each(data[0],function()...)
回答by raggle
The data in your JSON file is stored as an array within an array. Try this:
JSON 文件中的数据存储为数组中的数组。试试这个:
$(document).ready(function() {
$.ajax({
type: 'GET',
url: 'http://url',
dataType: 'json',
success: function(data) {
for (var i = 0; i < data.length; i++) {
var items = data[i];
$('#results').append('<div class="block"></div>');
var $block = $('#results').find('.block').last();
for (var j = 0; j < items.length; j++) {
var item = items[j];
var label = item.label;
var value = item.value;
$block.append('<p>' + label + '</p>' + '<p>' + value + '</p>');
}
}
}
});
return false;
});
回答by Raul Cruz
Thank you guys for your answers. It has helped me solve the issue. Each provide it some useful info and with a combination of all answers I got the code to work this way
谢谢你们的回答。它帮助我解决了这个问题。每个都提供一些有用的信息,并结合所有答案,我得到了以这种方式工作的代码
$(document).ready(function(){
$.ajax({
type: 'GET',
url: 'http://url',
dataType: 'json',
success: function(data) {
for (var i = 0; i < data.length; i ++ ){
$.each(data[i], function(index, items) {
var label = items.label;
var value = items.value;
$('#results').append('<p>' + label + '</p>' + '<p>' + value + '</p>');
});
}
}
});
return false;
});
So I removed 'var = items' as Mat suggested. I also added 'data[0]' and params to my each function like user733421 suggested, but that only display the first array objects. I have close to 200 arrays with several objects on each. So to loop through the whole data I added the 'for' statement as 0x12 suggested.
所以我按照 Mat 的建议删除了“var = items”。我还像 user733421 建议的那样在我的每个函数中添加了 'data[0]' 和 params,但这仅显示第一个数组对象。我有将近 200 个数组,每个数组都有几个对象。因此,为了遍历整个数据,我按照 0x12 的建议添加了“for”语句。
Now, all the data is displaying, but can somebody confirm that the way I did it is proper.
现在,所有数据都在显示,但有人可以确认我这样做的方式是正确的。
Again thank you guys great teamwork.
再次感谢你们伟大的团队合作。
回答by Coder John
$.each(data, function(key, items) {
var label = items.label;
var value = items.value;
}
Remove var items = []; from your code
删除 var items = []; 从你的代码
Rest should work hopefully
休息应该有希望