ionic 3 HttpClient get status + send headers

我与影子孤独终老i 提交于 2019-12-24 21:58:55

问题


i'm using the HttpClientModule on ionic 3, and i want to do a get on my api

let email = "test@email.com";
let password = "password";
    let headers = new HttpHeaders();
    this.http.get('http://127.0.0.1:8000/api/login',{
      headers: {'email':email,'password':password}
   });

i don't want to get the json but only the status of the request, for doing something like :

if(status == 200) { ... }
esle { ... } 

can you guys please help me ?

Thank you


回答1:


Add observe : 'response' in header options of http.get method and subscribe to get() method to get response status whatever you want..

this.http.get('http://127.0.0.1:8000/api/login',{
      headers: {'email':email,'password':password},observe: 'response'
   }).subscribe(
    res => { console.log(res) ;

             if(res.status==201)
             {
                 //do action
             }else
             {
             }
          },



回答2:


You have to use observe: 'response' as you second parameter in the Get request of httpClientModule like this -

this.http.get('http://127.0.0.1:8000/api/login',{
      headers: {'email':email,'password':password},
      observe: 'response'
   }

observe : 'response' for complete response.

observe : 'body' for response with body.

observe : 'events' for response with events. For more details on this refer here

  • https://www.concretepage.com/angular-2/angular-httpclient-get-example#observe


来源:https://stackoverflow.com/questions/50591970/ionic-3-httpclient-get-status-send-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!