javascript Jquery Ajax Json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7196498/
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 Object
提问by Shane Chin
Basically I am trying to return a list of names gotten from an Ajax request. When there is only one name, it works perfectly. However with multiple names I start seeing behavior I can't explain.
基本上,我试图返回从 Ajax 请求中获取的名称列表。当只有一个名字时,它完美地工作。但是,对于多个名称,我开始看到无法解释的行为。
function getIDFromInput(input){
sendToID = new Array; //An Array of "Name :Id"
$.ajax({
type:"GET",
url: "user_search.php",
contentType:"application/x-www-form-urlencoded; charset=utf-8",
dataType:"json",
async:false,
data: "q="+input,
success:function(data){
if(data.success){
var userLength = data.success.length;
if(userLength == 1){ // For one user everything works fine
var userNum = data.success.users[0];
var userName = data.success.usersNames[userNum];
sendToID[0] = userName + " :"+userNum;
}
else if(userLength > 1){ // Multiple users it fails
for(i = 0; i < userLength; i++){
var userNum = data.success.users[i];
//this call works
var userName = data.success.usersNames[userNum];
//this call fails, even though it seems to be the same call as above
sendToID[i] = userName + " :"+userNum;
}
}
else if(userLength < 1){ // never enter here
}
}
},
error:function(data){ //After it fails it goes into here
}
});
return sendToID;
}
The JSON I am passing back for < 2, ( The one that doesn't work, is below)
我为 < 2 传回的 JSON,(不起作用的,在下面)
{"success":{"length":2,"userNames":[{"5":"Travis Baseler"},{"6":"Ravi Bhalla"}],"users":["5","6"]}}
The JSON I am passing back the one that does work is
我传回有效的 JSON 是
{"success":{"length":"1","usersNames":{"6":"Ravi Bhalla"},"users":["6"]}}
Does anyone know why the first works but the second doesn't?
有谁知道为什么第一个有效而第二个无效?
回答by J. Ed
in your first example, "usernames"
is an array, and in the seconds one it's an object
(notice the []
in the first example which don't exist in the second one).
see @meagar's comment which explains this better than I did.
在您的第一个示例中,"usernames"
是一个数组,而在第二个示例中,它是一个对象
(注意[]
第一个示例中的 不存在于第二个示例中)。
请参阅@meagar 的评论,它比我更好地解释了这一点。
some further points:
1. you're using numbers as object property names; this (IMO) is not recommended since it's a bit confusing.
2. you can obtain the length of the array using the .length
property of an array:var userNum = data.success.users.length
3. wouldn't it make more sense to have your objects in the format of { 'userNum': X, 'username': Y }
? that way you can return just one array:success: [ {'userNum': 5, 'username': 'Travis Baseler'}, {'userNum': 6, 'username': 'Ravi Bhalla'}]
其他几点:
1. 您使用数字作为对象属性名称;不建议使用此 (IMO),因为它有点令人困惑。
2. 您可以使用数组的.length
属性获取数组的长度:var userNum = data.success.users.length
3. 将您的对象设为 格式不是更有意义{ 'userNum': X, 'username': Y }
吗?这样你就可以只返回一个数组:success: [ {'userNum': 5, 'username': 'Travis Baseler'}, {'userNum': 6, 'username': 'Ravi Bhalla'}]
回答by scrappedcola
Your for loop should look like this:
您的 for 循环应如下所示:
for(i = 0; i < userLength; i++){
var userNum = data.success.users[i];
//this call works
var userName = data.success.userNames[i][userNum];//you need to index the user in the array in the object uisng the loop then user the userNum to get your userName.
sendToID[i] = userName + " :"+userNum;
}