javascript - How can i get value of array using ajax function? - Stack Overflow

i have json array and this is my code var conf_url = "https:192.168.236.33confbridge_participan

i have json array

and this is my code

 var conf_url = "https://192.168.236.33/confbridge_participants/conference_participants.json?cid=0090000007";

   getParticipant(conf_url, function(output){
        var uid = output.uid;
        document.write(uid);
        });

 function getParticipant(conf_uri, handleData) {
    $.ajax({
      type: "GET",
      url: conf_uri,
      dataType: "jsonp",
      jsonpCallback: 'callback',
      contentType: "application/javascript",
      success: function(data) {
        handleData(data);
     //   console.log(data);
      }
    });

  }

i want to get uid of each object

but my output is undefined . . What am i doing wrong ? Please help.

i have json array

and this is my code

 var conf_url = "https://192.168.236.33/confbridge_participants/conference_participants.json?cid=0090000007";

   getParticipant(conf_url, function(output){
        var uid = output.uid;
        document.write(uid);
        });

 function getParticipant(conf_uri, handleData) {
    $.ajax({
      type: "GET",
      url: conf_uri,
      dataType: "jsonp",
      jsonpCallback: 'callback',
      contentType: "application/javascript",
      success: function(data) {
        handleData(data);
     //   console.log(data);
      }
    });

  }

i want to get uid of each object

but my output is undefined . . What am i doing wrong ? Please help.

Share Improve this question asked Aug 18, 2015 at 6:52 ar emar em 4442 gold badges6 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

you have iterate through the object list to output the value. You are getting an object array.

so

data.forEach(function(obj){
    console.log(obj['uid'])
})

the following would output the uid.

Update

You are passing a callback function to the Ajax request. So just do the following way.

getParticipant(conf_url, function(data) {
    data.forEach(function(obj){
       document.write(obj['uid'])
    })
});

function getParticipant(conf_uri, handleData) {
    $.ajax({
        type: "GET",
        url: conf_uri,
        dataType: "jsonp",
        jsonpCallback: 'callback',
        contentType: "application/javascript",
        success: handleData(data);
    });
}

pass the callback function directly to the ajax success and you can access the data returned from the ajax inside the callback function.

How about this:

 console.log(Object.keys(data).map(function(value, index){ return data[value].uid; }));

You need a loop to read the uid of each object. Your code now is reading the uid attribute on the Array object, that's why you are getting an undefined

var response = JSON.parse(data);
response.forEach(function(item){
    console.log(item.uid);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744834380a4596175.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信