代码之家  ›  专栏  ›  技术社区  ›  jyoseph

从函数返回的访问数组-javascript/jquery noob moment

  •  0
  • jyoseph  · 技术社区  · 15 年前

    提交表单时,我将调用一个函数getposts并传递一个变量str。我想做的是从该函数中获取返回的数据。

     // when the form is submitted
     $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
      var something = getPosts(str);
    
      * This is where I'd like to get the data returned from getPosts()
    
      return false;
     });
    
     // get the data
        function getPosts(str){
    
            $.getJSON('http://myurl.com/json?'+str+'&callback=?',
              function(data) {
    
                 arrPosts = new Array();
    
                  $.each(data.posts, function(i,posts){
    
                    // build array here
    
                  });
    
                  return arrPosts;
    
              });
        };
    

    我试过很多次了,但只得到了“未定义”的返回。我尝试过console.log(something);,console.log(getposts)。

    我错过了一些非常重要的东西。任何帮助都将不胜感激。

    编辑:

    我要做的是创建一个函数来获取文章。然后不同的事件会调用这个函数。然后我可以使用这些数据。因此,一个事件可能提交表单,另一个事件可能单击链接,另一个懒惰/无休止的滚动。所有人都可以使用相同的getposts函数。

    对结果进行了大量的分析,结果涉及到大量的代码行。只是想找到一种重用该函数的方法。你认为这是可能的吗?

    $('a.thisLink').click(function(){
        getPosts();
        get the return from getPosts() and do something with it
    });
    
    $('form.thisForm').submit(function(){
        getPosts();
        get the return from getPosts() and do something with it
    });
    
    function getPosts(){
       get the posts and return an array
    }
    
    3 回复  |  直到 15 年前
        1
  •  4
  •   Christian C. Salvadó    15 年前

    Ajax请求是异步执行的,回调函数( function (data) )当请求结束时执行of getjson,并且在该回调中返回一个值没有任何效果,因为它是getposts中的嵌套函数,其返回值从未被使用。

    实际上,在您的示例中,getPosts不返回任何内容,它结束了它的执行。 之前 返回数据。

    我建议您使用提交事件处理程序,如果要保留getposts函数,可以引入一个回调参数:

    $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
    
      getPosts(str, function (data) {
        var array = [];
        $.each(data.posts, function(i,posts){
          // build array here
          array.push(/* value to add */);
        });
        // array ready to work with...
        //...
      });
      return false;
    });
    
    function getPosts(str, callback){
      $.getJSON('http://myurl.com/json?'+str+'&callback=?', callback);
    }
    

    编辑2: 作为对第二条注释的响应,您可以进行另一个回调,该回调将在第一个回调处理完数据后执行,并且您可以在提交事件处理程序上执行getposts函数时定义它:

    $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
    
      getPosts(str, reusableCallback, function (result) {
        // result contains the returned value of 'reusableCallback' <---
      });
      return false;
    });
    
    function reusableCallback(data) {
      var array = [];
      $.each(data.posts, function(i,posts){
        array.push(/* value to add */);
      });
      //...
      return array;
    }
    
    function getPosts(str, callback, finishCallback){
      $.getJSON('http://myurl.com/json?'+str+'&callback=?', function (data) {
        finishCallback(callback(data)); // pass the returned value
                                        // of callback, to 'finishCallback' which is
                                        // anonymously defined on the submit handler
      });
    }
    

    编辑3: 我认为 getPosts 函数和“ reusableCallback “函数是强相关的,您可能希望加入它们,并使代码更易于使用和理解:

    $('form#getSome').submit(function(){
      var str = $("form#getSome").serialize();
    
      getPosts(str, function (result) {
        // result contains the processed results
      });
    
      return false;
    });
    
    
    function getPosts(str, finishCallback){
      $.getJSON('http://myurl.com/json?'+str+'&callback=?', function (data) {
        // process the results here
        var array = [];
        $.each(data.posts, function(i,posts){
          array.push(/* value to add */);
        });
        //...
        finishCallback(array); // when the array is ready, execute the callback 
      });
    }
    
        2
  •  0
  •   camomileCase    15 年前

    您的getposts函数看起来不完整,我不是jquery专家,但它应该看起来像:

    function getPosts(str) {
    
      $.getJSON('http://myexample.com/json?'+str+'&callback=?',function(data){
    
        var arrPosts = [];
    
        $.each(data.posts, function(i,posts){
         ... build array yada yada ...
        });
    
        return arrPosts;
    
      });
    }
    
        3
  •  0
  •   John Boker    15 年前

    问题是,当get请求返回数据时,会调用$.getjson回调函数,而不是与函数内联。