Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用前一个Ajax请求中的数据生成Ajax请求_Ajax_Angularjs - Fatal编程技术网

使用前一个Ajax请求中的数据生成Ajax请求

使用前一个Ajax请求中的数据生成Ajax请求,ajax,angularjs,Ajax,Angularjs,我正试图编写一个有角度的页面来与我的Nodejs服务器通信,但遇到了一个障碍 我需要使用多个Ajax请求,这些请求依赖于以前Ajax请求中的数据来工作 因此,Ajax请求#1提供所有其他Ajax请求使用的数据,Ajax请求#2使用来自Ajax请求#1的数据来获取Ajax请求#3需要的数据 由于Angular是异步的,如何让脚本在进行下一次ajax调用之前等待第一次调用的数据 id=ajax() 等待数据 令牌=ajax(id) 等待数据 gametoken=ajax(id,令牌) 等待数据Cha

我正试图编写一个有角度的页面来与我的Nodejs服务器通信,但遇到了一个障碍

我需要使用多个Ajax请求,这些请求依赖于以前Ajax请求中的数据来工作

因此,Ajax请求#1提供所有其他Ajax请求使用的数据,Ajax请求#2使用来自Ajax请求#1的数据来获取Ajax请求#3需要的数据

由于Angular是异步的,如何让脚本在进行下一次ajax调用之前等待第一次调用的数据

id=ajax()

等待数据

令牌=ajax(id)

等待数据

gametoken=ajax(id,令牌)


等待数据

Chandermani是正确的,请记住确保所需变量在所需范围内可用

var id,token,gametoken;
$http.get('http://host.com/first')
   .then(function(result){
       id=result;
       return $http.get('http://host.com/second/'+id);
    }
    .then(function(result){
        token = result
        return $http.get('http://host.com/third'+id+'/'+token);
    }
    .then(function(result){
        gametoken = result;
        //Do other code here that requires id,token and gametoken
    }
编辑: 你不必把承诺连锁反应。如果您想在以后的某个日期打电话,并且希望确保承诺已得到解决,则可以使用$q.all()


$q.all()接受一个数组,因此您可以根据需要放入多个承诺,并等待它们全部解决。

基本上您需要阅读angularJS承诺。并使用$http服务链接承诺。看到这篇文章,这并不能帮助我解决问题,因为我不确定下一个数据请求何时发生。这可能发生在3秒后,也可能发生在10分钟后,这取决于用户选择做什么。
var id,token,gametoken;
var p1 = $http.get('http://host.com/first')
   .then(function(result){
       id=result;
    }

// Later on to make your new second call
 $q.all([p1]).then(function(){
      //Make second call knowing that the first has finished.
 }