2017-05-25 39 views
0

我有一個驗證用戶的問題。如果憑證(電子郵件和密碼)出現問題,我會顯示一條包含敬酒的錯誤消息。但從今天上午開始,我出現了401未經授權的錯誤,因此吐司不再顯示錯誤消息。我不明白它來自哪裏。401 Ionic 2中POST請求的未授權錯誤?

控制檯的錯誤

POST UrlAPI 401 (Unauthorized) 
EXCEPTION: Response with status: 401 Unauthorized for URL: UrlAPI 

這是我的POST請求

return this.http.post(link, data, options) 
    .map((res: Response) => res.json()) 
    .subscribe((response) => { 
      console.log("RESPONSE: ", response); 
      if (response.status === 'ok'){ 
       this.UserData.setSessionId(response.session_id); 
       console.log("SESS_ID SAVED", this.UserData.getSessionId()); 
       this.nav.setRoot(TabsPage); 
      } 
      else { 
       let toast = this.toastCtrl.create({ 
        message: response.errors, 
        duration: 3000, 
        position: 'top' 
       }); 
      toast.present(); 
      } 
     }); 

效應初探頭

Access-Control-Allow-Origin:* 
Cache-Control:no-cache 
Connection:Keep-Alive 
Content-Length:57 
Content-Type:application/json 
Date:Tue, 23 May 2017 13:49:56 GMT 
Keep-Alive:timeout=5, max=100 
Server:Apache/2.4.10 (Debian) 

請求頭

Accept:*/* 
Accept-Encoding:gzip, deflate, br 
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4 
Connection:keep-alive 
Content-Length:16 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Host: UrlAPI 
Origin:http://localhost:8101 
Referer:http://localhost:8101/?ionicplatform=ios&ionicstatusbarpadding=true 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 

回答

0

的問題是,當比2xx的狀態碼從服務器,或者從服務器返回的時間的角度HTTP服務引發錯誤。

這是通過將.MAP和.subscribe之間可觀察到的捕獲,或通過添加一個第二參數的訂閱,這是一種錯誤處理程序處理。

溶液1

.map(...) 
.catch(/* handle error here */) 
.subscribe(...) 

溶液2

.map(...) 
.subscribe( 
    (response) => { ... }, 
    (error) => { /*handle error here */ } 
) 

參見這個類似的問題中的示例: How to deal with http status codes other than 200 in Angular 2