2016-09-15 6 views
0

角度インターセプターを使用していて、メッセージから500エラー(内部サーバーエラー)を取得したいとします。角度インターセプター - 500エラーからメッセージを取得

問題は、私はインターセプタ内部rejection.dataでresponseError(下のスクリーンショット)で全体のHTMLを取得していていること、です。

私は構成する必要があることを読んだweb.configしかし、私はまだ全体のHTMLを取得しています。メッセージを受け取りたいだけです。

これは可能ですか?

角度インターセプター:

app.config(['$httpProvider', function ($httpProvider) { 

    $httpProvider.interceptors.push(function ($q, $rootScope) { 

     return { 
      request: function (config) { 

       //the same config/modified config/a new config needs to be returned. 
       return config; 
      }, 
      requestError: function (rejection) { 

       //Initializing error list 
       if ($rootScope.errorList == undefined) { 
        $rootScope.errorList = []; 
       } 

       $rootScope.errorList.push(rejection.data); 

       //It has to return the rejection, simple reject call doesn't work 
       return $q.reject(rejection); 
      }, 
      response: function (response) { 

       //the same response/modified/or a new one need to be returned. 
       return response; 
      }, 
      responseError: function (rejection) { 

       //Initializing the error list 
       if ($rootScope.errorList == undefined) { 
        $rootScope.errorList = []; 
       } 

       //Adding to error list 
       $rootScope.errorList.push(rejection.data); 

       //It has to return the rejection, simple reject call doesn't work 
       return $q.reject(rejection); 
      } 
     }; 
    }); 
}]); 

のWeb.Configを編集

<system.webServer> 
    <httpErrors existingResponse="PassThrough" errorMode="Detailed"></httpErrors> 
</system.webServer> 

Image

:私は例外スナップショットからのメッセージを取得したい

Image2

+0

あなたはdata.messageになっていますか? – Disha

+0

何もありません。データでは、私は最初の画像のように全体のHTMLを持っているので。私は編集をしました。 –

答えて

0

私は500エラーからメッセージを取得したい(内部サーバーエラー)。 {string|Object} - - レスポンスボディ

  • データ:

    レスポンスオブジェクトはこれらのプロパティがあります。ドキュメントから

    responseError: function (errorResponse) { 
    
        //Initializing the error list 
        if ($rootScope.errorList == undefined) { 
         $rootScope.errorList = []; 
        } 
    
        //Adding to error list 
        $rootScope.errorList.push(errorResponse.statusText); 
    
        //It has to return the rejection, simple reject call doesn't work 
        return $q.reject(errorResponse); 
    } 
    

    使用response.statusTextは、メッセージを取得します変換関数で変換されます。

  • ステータス - {number} - 応答のHTTPステータスコード。
  • ヘッダー - {function([headerName])} - ヘッダーゲッター機能。
  • config - {Object} - 要求を生成するために使用された構成オブジェクト。
  • statusText - {string} - レスポンスのHTTPステータステキスト。

からAngularJS $http Service API Reference -- General Usage

関連する問題