admin管理员组

文章数量:1293936

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.

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)

{"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

{"success":{"length":"1","usersNames":{"6":"Ravi Bhalla"},"users":["6"]}}

Does anyone know why the first works but the second doesn't?

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.

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)

{"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

{"success":{"length":"1","usersNames":{"6":"Ravi Bhalla"},"users":["6"]}}

Does anyone know why the first works but the second doesn't?

Share Improve this question asked Aug 25, 2011 at 20:01 Shane ChinShane Chin 5762 gold badges10 silver badges20 bronze badges 6
  • 4 You should be avoiding synchronous AJAX requests when possible – user229044 Commented Aug 25, 2011 at 20:05
  • 2 @meagar: I know I'm going out on a limb here, but I'm sure there is a reason for it being synchronous. If he didn't know anything about JS/jQuery he wouldn't even know the option exists. – josh.trow Commented Aug 25, 2011 at 20:08
  • Once you get the data format straightened out so it's the same for all cases, you don't need the if/else at all. You can do all three branches from the for loop. The for loop handles length of 0, length of 1 and length of > 1 all automatically. – jfriend00 Commented Aug 25, 2011 at 20:09
  • @josh Yet, here he is using synchronous AJAX calls. – user229044 Commented Aug 25, 2011 at 20:09
  • 1 @Shane Chin: AAAAAAAUGH you're killing me man :) It must have been a copy/paste job then... – josh.trow Commented Aug 25, 2011 at 20:39
 |  Show 1 more ment

2 Answers 2

Reset to default 7

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 ment which explains this better than I did.

some further points:
1. you're using numbers as object property names; this (IMO) is not remended 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'}]

Your for loop should look like this:

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;
 }  

本文标签: javascriptJquery Ajax Json ObjectStack Overflow