【问题标题】:AngularJS success function of promisePromise的AngularJS成功函数
【发布时间】:2016-04-15 07:24:30
【问题描述】:

我已对 URL 进行了 HTTP post API 调用。

我收到了回复,但我对如何编写成功函数感到困惑,因为它有很多方法。

这是我的 API 调用。请帮我看看success函数是怎样的?

var req = {
    method: 'POST',
    url: viewProfileurl,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + $rootScope.token,
    },
    params: {
        'action':'view'
    }
}

$http(req);

【问题讨论】:

    标签: angularjs angular-promise angular-http


    【解决方案1】:

    Angular 在$http 实现内部使用promise,即$q

    帮助您异步运行函数并使用它们的服务 完成处理后返回值(或异常)。

    所以,有两种选择:

    第一个选项

    您可以使用.success.error 回调:

    var req = {
      method: 'POST',
      url: viewProfileurl,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + $rootScope.token,
      },
      params: {
        'action': 'view'
      }
    
    }
    
    $http(req).success(function() {
        // do on response success
    }).error(function() {
    });
    

    .success.error 已被弃用。

    所以,选择第二个选项。

    第二个选项

    改用.then函数

    var req = {
      method: 'POST',
      url: viewProfileurl,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': 'Bearer ' + $rootScope.token,
      },
      params: {
        'action': 'view'
      }
    
    }
    
    $http(req).then(function() {
        // do on response success
    }, function() {
       // do on response failure
    });
    

    【讨论】:

      【解决方案2】:

      您需要编写一个成功回调来检索您的 API 返回的数据。

      $http(req)
           .then(function (response) {
               var data = resposne.data;
               ...
            }, function (error) {
               var errorStatusCode = error.StatusCode;
               var errorStatus = error.Status;
               ...
            });
      

      基本上$http返回一个promise,你需要写一个回调函数。

      或者你可以这样做:

      $http(req).success(function(respData) { var data = respData; ... });
      $http(req).error(function(err) { ... });
      

      【讨论】:

        【解决方案3】:

        这是成功与错误语法

            $http.get("/api/my/name")
          .success(function(name) {
            console.log("Your name is: " + name);
          })
          .error(function(response, status) {
            console.log("The request failed with response " + response + " and status code " + status);
          };
        

        使用那么

        $http.get("/api/my/name")
          .then(function(response) {
            console.log("Your name is: " + response.data);
          }, function(result) {
            console.log("The request failed: " + result);
          };
        

        【讨论】:

          【解决方案4】:

          $http 返回一个 Promise,其中包含您可以使用的 then 函数。

          $http(req).then(function (data) { ...; });
          

          then的定义:

          then(successCallback, failCallback)
          

          【讨论】:

            猜你喜欢
            • 2015-11-12
            • 1970-01-01
            • 2015-12-04
            • 2015-12-06
            • 2019-01-09
            • 1970-01-01
            • 1970-01-01
            • 2016-12-20
            • 2017-05-02
            相关资源
            最近更新 更多