2016-08-26 3 views
0

私は$httpProviderinterceptorsAngular HTTP Interceptorから応答ボディにアクセスする方法障害が発生しましたか?

function httpErrorHandler($q) { 
    return { 
     'responseError': function (response) { 
      if (response.status === 500) { 
       if (response.data && response.data.html) { 
        var msg = angular.fromJson(response.data.html).Data; 
        showStatusMessage(msg, 'error'); // assume available 
       } else { 
        showStatusMessage(response.statusText, 'error'); 
       } 
      } 
      // Always reject (or resolve) the deferred you're given 
      return $q.reject(response); 
     } 
    }; 
} 

と迎撃コードにプッシュしています。このfactoryを持っている:

function config($httpProvider) { 
    $httpProvider.interceptors.push('HttpErrorHandler'); 
} 

私は、サーバーからの応答失敗した、クロムデベロッパーツールで見を取得すると> [ネットワーク]タブでは、失敗したXHRと応答]タブを選択し、私はこれを取得:

{"Success":false,"Data":"Error Code 0000-229566."} 

どのようなステータスメッセージに表示するresponse.statusText

完全であることを試してみることです、ここではレスポンスヘッダを以下のとおりです。私が好きな何

Cache-Control:no-cache, no-store, must-revalidate 
Content-Length:110 
Content-Security-Policy:frame-ancestors 'self' 
Content-Type:application/json; charset=utf-8 
Date:Fri, 26 Aug 2016 16:26:11 GMT 
Expires:-1 
Pragma:no-cache 
Strict-Transport-Security:max-age=31536000; includeSubDomains 
X-Content-Type-Options:nosniff 
X-Content-Type-Options:nosniff 
X-Frame-Options:SAMEORIGIN 

は、「エラーコードを持つことである000から1234 "私のステータスメッセージに表示されますが、それは来ていません。私はそれをどのように表示することができますか?

答えて

0

の代わりに使用すると、レスポンスヘッダのSTATUSTEXTプロパティを読んでいる応答内容の.DATAプロパティにアクセスします。 statusTextは、応答のステータスコードの説明のようです。more herehttps://embed.plnkr.co/xz54Mh2zGguHB0cLquGE/ 私のエラーメッセージプロパティが.status_message代わりの.DATA

responseError: function (response) { 
/* 
    NOTE: if "status_message" is not in the parsing string JSON.parse(response.data.html).status_message 
    will thrown an undefined error and break the execution 
*/ 
var msg = !response.data ? 'Error occured ...' 
    : !response.data.html ? response.data.status_message : JSON.parse(response.data.html).status_message; 
$alert.show(msg, 'danger'); 

return $q.reject(response); 
} 
です:ワーキングサンプルをスクリプト

関連する問題