是否有可能在另一个请求中有一个Angular js $ http请求?

cod*_*ruo 1 html javascript http angularjs angularjs-http

我正在尝试发送请求AngularJS以获取用户注册的令牌.一旦我获得令牌,我将使用令牌发送另一个请求来注册用户.

但是第一个请求有效,我得到令牌,但第二个请求不起作用.

facebookExample.controller('registerController', function($scope, $http, $localStorage, $location) {
  $scope.data = {};
  $scope.getToken = function() {
      $http.post('http://you-serve.org/api.php?action=createaccount&format=json&name=' + $scope.data.username + '&email=' + $scope.data.email + '&realname=' +
        $scope.data.firstname + ' ' + $scope.data.lastname +
        '&mailpassword=false&reason=Fun_and_profit&language=en&token').then(
        function(response) {
          return response.data.createaccount.token;
        }
        // End success
        ,
        function() {
          alert('error');
        } // End error 
      ); //End then
    } // End getToken

  $scope.register = function(myToken) {
      $http.post('http://you-serve.org/api.php?    action=createaccount&format=json&name=' +
        $scope.data.username + '&email=' +
        $scope.data.email + '&realname=' +
        $scope.data.firstname + ' ' +
        $scope.data.lastname +
        '&mailpassword=false&reason=Fun_and_profit&language=en&token=' + myToken).then(
        function(response) {
          alert("Registration successful !  You can log in now " + response.data.createaccount.username);
        },
        function(response) {
          alert(response.data.error.info);
        }); //End then
    } //End register


  $scope.signup = function() {
      register(getToken);
    } // End sign up
}); // End controller
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<ion-view ng-controller="registerController" title="Sign Up" view-title="Sign Up" name="login-view">

  <ion-content class="padding">
    <div class="list list-inset">
      <label class="item item-input">
        <input type="text" placeholder="Username" ng-model="data.username">
      </label>
      <label class="item item-input">
        <input type="password" placeholder="Password" ng-model="data.password">
      </label>


      <label class="item item-input">
        <input type="text" placeholder="Email" ng-model="data.email">
      </label>


      <label class="item item-input">
        <input type="text" placeholder="First Name" ng-model="data.firstname">
      </label>

      <label class="item item-input">
        <input type="text" placeholder="Last Name" ng-model="data.lastname">
      </label>



      <label class="item item-input">
        <input type="text" placeholder="City" ng-model="data.city">
      </label>




      <label class="item item-input">
        <input type="text" placeholder="Country" ng-model="data.country">
      </label>
    </div>
    <button class="button button-block button-calm" ng-click="register(getToken())">Sign Up</button>
  </ion-content>
</ion-view>
Run Code Online (Sandbox Code Playgroud)

谁知道我怎么能这样做?

Ion*_*ica 7

您可以使用promise1.then(promise2)链接promises($ http返回的内容).

因此,在您的情况下,链接getToken和注册的正确方法是:

$scope.getToken().then($scope.register)
Run Code Online (Sandbox Code Playgroud)

(修改两个函数后返回 $ http调用)

编辑:(进一步说明)

你要做的是被称为承诺链.实际上,按顺序执行两个或多个异步操作.来自html5rocks承诺教程:

排队异步操作

您还可以链接"然后"以按顺序运行异步操作.

当你从"then"回调中返回一些东西时,它有点神奇.如果返回一个值,则使用该值调用下一个"then".但是,如果你返回类似promise的东西,那么下一个"then"就会等待它,并且只有当这个承诺解决时才会被调用(成功/失败).

它的工作方式是当你从函数Promise.then()函数返回一个值时,然后用函数的返回值.then()或者分辨率调用链中的下一个函数Promise,如果返回值为a Promise,则链然后在Promise继续之前等待此解决.

因此(并记住比$ http返回一个Promise对象):

function a() {
    return $http.get('...').then(function (response) { return response.data; });
}

function b(data) {
    return $http.get('...'+data).then(function (response) { return response.data; });
}
Run Code Online (Sandbox Code Playgroud)

要将两个函数链接在一起,以便b()使用结果调用a(),只需执行以下操作:

a().then(b)
Run Code Online (Sandbox Code Playgroud)

请阅读s的教程,Promise以便更好地处理它们的工作方式.它们是JavaScript使异步操作更容易处理的最佳工具之一.

PS:在您的具体情况下,代码将是:

facebookExample.controller('registerController', function($scope, $http, $localStorage, $location) {
  $scope.data = {};

  function getToken() {
    return $http.post('http://you-serve.org/api.php?action=createaccount&format=json&name=' + $scope.data.username + '&email=' + $scope.data.email + '&realname=' + $scope.data.firstname + ' ' + $scope.data.lastname + '&mailpassword=false&reason=Fun_and_profit&language=en&token')
      .then(
        function(response) {
          return response.data.createaccount.token;
        },
        // End success
        function() {
          alert('error');
        } // End error 
      ); //End then
  } // End getToken

  function register(myToken) {
    return $http.post('http://you-serve.org/api.php?action=createaccount&format=json&name=' +
        $scope.data.username + '&email=' +
        $scope.data.email + '&realname=' +
        $scope.data.firstname + ' ' +
        $scope.data.lastname +
        '&mailpassword=false&reason=Fun_and_profit&language=en&token=' + myToken)
      .then(
        function(response) {
          alert("Registration successful !  You can log in now " + response.data.createaccount.username);
        },
        function(response) {
          alert(response.data.error.info);
        }
      ); //End then
  } //End register


  $scope.signup = function() {
    getToken().then(register);
  } // End sign up
}); // End controller
Run Code Online (Sandbox Code Playgroud)