如何从jQuery ajax成功函数正确返回数组? [重复]

时间:2022-10-08 07:40:39

This question already has an answer here:

这个问题在这里已有答案:

TheObject = {

TheObject = {

    getArray: function(){
        var groups = new Array;
        $.ajax({
              type: "POST",
              url: "link.php",
              success: function (data){
                  var counter = 0;
                  $('g',data).each(function(){    
                      var group_name = $(this).find("name").text();
                      var group_id = $(this).find("id").text();
                      var group = {
                         id: group_id,
                         name: group_name
                      }
                      groups[counter] = group;
                      counter++;
                  });
                  return groups;
              }
         });
     }

}

And when I try to call this method:

当我尝试调用此方法时:

var a = TheObject.getArray();
alert(a);

It returns 'undefined'. I cant figure out where is the problem. The array gets created inside the success function but I'am unable to return it properly. Thanks for your help!

它返回'undefined'。我无法弄清问题在哪里。数组在成功函数内创建,但我无法正确返回。谢谢你的帮助!

3 个解决方案

#1


16  

In your code, you are looking for groups using procedural coding after the ajax call was made. The main problem is that you are looking for groups before the ajax call is complete.

在您的代码中,您在进行ajax调用后正在寻找使用过程编码的组。主要问题是你在ajax调用完成之前正在寻找组。

Another problem is that you are returning groups to the success() function, but the TheObject.getArray() function returns nothing.

另一个问题是您将组返回到success()函数,但TheObject.getArray()函数不返回任何内容。

So you need to bring in the callback into the ajax function like this:

所以你需要将回调引入ajax函数,如下所示:

TheObject = {
    getArray: function(callback) {
        var groups = new Array;
        $.ajax({
              type: "POST",
              url: "link.php",
              success: function (data){
                  var counter = 0;
                  $('g',data).each(function(){    
                      var group_name = $(this).find("name").text();
                      var group_id = $(this).find("id").text();
                      var group = {
                         id: group_id,
                         name: group_name
                      }
                      groups[counter] = group;
                      counter++;
                  });
                  callback.call(this,groups);
              }
         });
     }
}

TheObject.getArray(function(a) {
    // this code runs when the ajax call is complete
    alert(a);
});

#2


5  

A very simple version of David's example.

David的一个非常简单的例子。

TheObject = {
    getArray: function(callback) { 
        $.ajax({
              cache: true,
              type: "GET",
              url: "http://www.domain.com/core/domains.php",
              success: function (data){ 
                  callback.call(this,data);
              }
         });
     }
}

TheObject.getArray(function(data) {
    javascript: console.log(data);    
});

#3


0  

Use push on the array. Also you want to create a type called Group and then create a new group in the loop and then push it into the array.

使用push on the array。您还想创建一个名为Group的类型,然后在循环中创建一个新组,然后将其推入阵列。

#1


16  

In your code, you are looking for groups using procedural coding after the ajax call was made. The main problem is that you are looking for groups before the ajax call is complete.

在您的代码中,您在进行ajax调用后正在寻找使用过程编码的组。主要问题是你在ajax调用完成之前正在寻找组。

Another problem is that you are returning groups to the success() function, but the TheObject.getArray() function returns nothing.

另一个问题是您将组返回到success()函数,但TheObject.getArray()函数不返回任何内容。

So you need to bring in the callback into the ajax function like this:

所以你需要将回调引入ajax函数,如下所示:

TheObject = {
    getArray: function(callback) {
        var groups = new Array;
        $.ajax({
              type: "POST",
              url: "link.php",
              success: function (data){
                  var counter = 0;
                  $('g',data).each(function(){    
                      var group_name = $(this).find("name").text();
                      var group_id = $(this).find("id").text();
                      var group = {
                         id: group_id,
                         name: group_name
                      }
                      groups[counter] = group;
                      counter++;
                  });
                  callback.call(this,groups);
              }
         });
     }
}

TheObject.getArray(function(a) {
    // this code runs when the ajax call is complete
    alert(a);
});

#2


5  

A very simple version of David's example.

David的一个非常简单的例子。

TheObject = {
    getArray: function(callback) { 
        $.ajax({
              cache: true,
              type: "GET",
              url: "http://www.domain.com/core/domains.php",
              success: function (data){ 
                  callback.call(this,data);
              }
         });
     }
}

TheObject.getArray(function(data) {
    javascript: console.log(data);    
});

#3


0  

Use push on the array. Also you want to create a type called Group and then create a new group in the loop and then push it into the array.

使用push on the array。您还想创建一个名为Group的类型,然后在循环中创建一个新组,然后将其推入阵列。