如何在AJAX中加载结果输出?

时间:2015-11-23 02:52:04

标签: javascript jquery ajax

我使用javascript有这个OOP但结果数据是未定义的。也许有人可以帮我解决这个问题。

var OOP = OOP || {
      vars: {},
      elems: {},
      fn: {
        loadData: function() {
         $.post('URLOFPHP', 
          {} //This is blank because the URL Automatically loads all the resulted queries
          ).done(function(response){
            var newData = $.parseJSON(response); 
            return newData;
            //console.log(newData);
          });
        }
      }
};

$(function() {
  console.log(OOP.fn.loadData()); // This gives me undefined
});

但是当我在下面尝试时它起作用:

$(function() {
 $.post('URLOFPHP', 
  {} //This is blank because the URL Automatically loads all the resulted queries
 ).done(function(response){
    var newData = $.parseJSON(response); 
    //return newData;
    console.log(newData);
 });
});

新数据的Console.log是:

[Object, Object, Object]
  0: Object
     Name: "Test1"
     >_proto_: Object
  1: Object
     Name: "Test2"
     >_proto_: Object
  2: Object
     Name: "Test3"
     >_proto_: Object
   length: 3

0 个答案:

没有答案