jQuery Ajax调用,返回值问题 [英] JQuery Ajax call, return value problem

查看:78
本文介绍了jQuery Ajax调用,返回值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function getMore(from){
 var initData = "&start-index=";
 initData += from;
 $.ajax({
  type:"POST",
  url: '', //removed the URL
  data: initData,
  dataType: 'json',
  success: function(result) {
   return result;
  },
  error: function(errorThrown) {

  }
 });

 return result;
}

它是一个Google基本查询;我还有另一个函数,可以进行初始服务器调用并获取前250个项目.然后,我有了一个运行中的计数器,只要结果= 250,它就会再次调用服务器,但是从当前提取的项目数量的"start-index ="开始.这部分都可以正常使用,并且与Firebug一起使用,我还可以看到服务器响应是正确的JSON.

Its a google base query; I have another function that makes the initial server call and gets the first 250 items. I then have a running counter and as long as the results = 250 it calls the server again, but starting at "start-index=" of the current amount of items pulled off. This part all works correctly and with firebug I can also see that the server response is proper JSON.

我遇到的麻烦是试图将JSON从此函数返回到调用它的函数.我不想再次调用原始函数,因为它将清除已经从服务器提取的数据数组.每次返回到父函数都为空.

The trouble I'm having is trying to return the JSON from this function to the function that called it. I do not want to call the original function again because it will clear the arrays of data already pulled from the server. Each time it returns to the parent function it's null.

有人知道我如何使用返回"来返回数据吗?

Does anyone know how i can go about returning the data using "return"?

推荐答案

function FuncionCallGetMore(){
    //...    
    getMore('x-value', FuncionGetReturn);
    //...
}  

function FuncionGetReturn(error, value){
   if (!error) {
       // work value
   }
}

function getMore(from, fn){

  var initData = "&start-index=" + from;

  $.ajax({
    type:"POST",
    url: '', //removed the URL
    data: initData,
    dataType: 'json',
    success: function(result) {
      fn(false, result);
    },
    error: function(errorThrown) {
      fn(true);
    }


    });

    return;
}

这篇关于jQuery Ajax调用,返回值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆