2017-05-25 102 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