问题
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