weixin_33709364 2011-08-25 20:01 采纳率: 0%
浏览 24

jQuery Ajax Json对象

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?

  • 写回答

2条回答 默认 最新

  • weixin_33704591 2011-08-25 20:05
    关注

    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.

    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'}]

    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿