[英]Angular 2 http post is returning 200 but no response is returned
My http call is returning 200 but no response is captured. 我的http呼叫返回200,但未捕获任何响应。 My code inside subscribe is not being hit .
我的订阅里面的代码没有被击中 。 The API is returning data when I test in postman.
当我在邮递员中测试时,API返回数据。 Here is my code.
这是我的代码。
getToken(authcode: string) {
var data = 'client_id=InspectWebApp_client&code=' + authcode + '&redirect_uri=http://localhost:3000&grant_type=authorization_code';
let headers = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
let options = new RequestOptions({ headers: headers });
this.http.post('https://fedloginqa.test.com/as/token.oauth2', data, options)
.subscribe((res: Response) => {
var resultsToken = res.json();
localStorage.setItem("access_token",resultsToken.access_token)
//return this.inspections;
})
}
I was also facing the same problem. 我也面临着同样的问题。 The problem was solved using the map function on Observables.
使用Observables上的map函数解决了该问题。 Here is my implementation:
这是我的实现:
login(Username:string, Password:string) : Observable<Response>{
let headers = new Headers();
headers.append("Authorization", "Basic " + btoa(Username + ":" + Password));
headers.append("Content-Type", "application/x-www-form-urlencoded");
return this._http.post(this._baseUrl+"auth/login", " " , {headers: headers} )
.map((response: Response) => {
return response;
}).catch(this.handleError);
}
Here the handleError is a function to catch the excceptions generated. 这里的handleError是捕获生成的感知的函数。 This is a function in login.service.ts that sends the username and password to the api to get data.
这是login.service.ts中的一个函数,该函数将用户名和密码发送到api以获取数据。 You can see that I am returning response from the map function in this service.
您可以看到我正在从此服务中的map函数返回响应。 Now, this returned response can be caught in subscribe function in following way:
现在,可以通过以下方式在订阅函数中捕获此返回的响应:
this._loginService.login(this.username, this.password)
.subscribe(
(response) => {
//Here you can map the response to a type.
this.apiResult = <IUser>response.json();
},
(err) => {
//Here you can catch the error
},
() => {this.router.navigate(['home'])}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.