怎么用。然后像.success?

时间:2015-10-20 09:28:02

标签: angularjs

我有这样的代码。但那会返回OLD令牌,虽然我的服务器有NEW令牌。

function stores() {
    return $http.get(URL + 'stores').then(function(response){
        console.log(response.config.headers.Authorization);
    });
}

以下代码返回NEW令牌,因为我的服务器最后生成。这就是我想要的。

function stores() {
    return $http.get(URL + 'stores').success(function(data, status, headers, config){
        console.log(headers('Authorization'));
    });
}

问题是,我怎么能用.then方法做到这一点?我看看$ http doc,他们说.success将会减少

2 个答案:

答案 0 :(得分:1)

您的问题与您访问标题的方式有关。响应对象具有以下属性(from Angular docs):

  • data - .manualNotification #recipientsTab .email-row { margin-top: 5px; margin-bottom: 5px; } .manualNotification #recipientsTab .email-row label { display: inline-block; width: 50px; text-align: center; } .manualNotification #recipientsTab .email-row a { position: absolute; width: 50px; right: 10px; margin-left: 10px; } - 使用转换函数转换的响应主体。
  • status - {string|Object} - 响应的HTTP状态代码。
  • 标题 - {number} - 标题获取功能。
  • config - {function([headerName])} - 用于生成请求的配置对象。
  • statusText - {Object} - 响应的HTTP状态文本。

像这样访问标题:

{string}

答案 1 :(得分:0)

非常确定,因为.then需要两个回调函数。一次成功,一次失误。像这样:

$http.get( URL + 'stores').then(function(res){

        $scope.var = res.data;

    },function(){})