typescript
  • error-handling
  • aurelia
  • http-status-code-403
  • generic-programming
  • 2017-03-28 1 views 2 likes 
    2

    は別途catchブロック内にそれらのそれぞれを処理することなく、一般的な方法で、サーバからの送信403個の応答を処理する方法はありますか? `アウレリア(活字体)での一般的な方法で403エラーを処理

    //現在のコード

    searchCustomer(customerName: string): any { 
          if (customerName != "") { 
           let url = 'clinics/customers/lookup/name/' + customerName; 
           let headers = new Headers(); 
           headers.append('accept', 'application/vnd.vetserve.customerlookup.v1.hal+json'); 
           return this.http.fetch(url, { method: 'GET', headers: headers }) 
            .then(response => response.json()) 
            .catch(error => { 
             if(error.status==403){ 
              this._messageService.showMessage('No permission', MessageService.type.error, error); 
            } 
             console.log(error); 
            } 
             ); 
          } 
         }` 
    
    +0

    インターセプタの設定を見てください - http://aurelia.io/hub.html#/doc/article/aurelia/fetch-client/latest/http-services/3 – Tom

    +0

    実際には私に必要なのはメソッド呼び出し自動的に403が来るとき – Heshan

    +1

    はい - インターセプタを使用してヘッダーのステータスを確認し、必要な方法を呼び出すことができます。 – Tom

    答えて

    1

    おかげでinterseptionsを示唆するために、これが最良の方法である@thebluefoxと私は必要、API-client.tsファイルは、我々は.withInterceptorでその レスポンス(応答)以下のような条件がエラー

    をcatchs変更することができますクラスApiClientを得ました
    export class ApiClient { 
         http:HttpClient = null; 
    
         constructor(aurelia:Aurelia, auth:AuthenticationService) { 
         let httpClient = new HttpClient(); 
         httpClient.configure(httpConfig => { 
          httpConfig 
          .withDefaults({ 
           headers: { 
           'Accept': 'application/json' 
           } 
          }) 
          .withInterceptor({ 
           request(request) { 
            if (!auth.isAuthenticated()) { 
             aurelia.setRoot('authentication'); 
            }; 
           request.headers.append('Authorization', 'bearer ' + auth.accessToken); 
    
           return request; 
           } 
           response(response) { 
           console.log(`Received ${response.status} ${response.url}`); 
           return response; // you can return a modified Response 
    
           } 
          }) 
          .useStandardConfiguration() 
          .withBaseUrl(config.api_endpoint); 
         }); 
         this.http = httpClient; 
         } 
        } 
    
    関連する問題