簡體   English   中英

如何使用.then就像.success一樣?

[英]How to use .then like .success?

我有這樣的代碼。 但這返回了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將會淘汰

您的問題出在訪問標頭的方式上。 響應對象具有以下屬性( 來自Angular docs ):

  • data – {string|Object} –使用轉換函數轉換的響應主體。
  • status – {number} –響應的HTTP狀態代碼。
  • 標頭– {function([headerName])} –標頭獲取函數。
  • config – {Object} –用於生成請求的配置對象。
  • statusText – {string} –響應的HTTP狀態文本。

像這樣訪問標題:

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

可以肯定,因為.then需要兩個回調函數。 一成功一錯誤。 像這樣:

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

        $scope.var = res.data;

    },function(){})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM