This same code was working on Angular 2, but on Angular 6 it returns error on http (i.e the http fails to the error handler) but request is success (Code:200 and returning the data as expected)
Update Summary of the issue it was basically cors issue
1- I was using Angular2 HTTP instead HttpClient applied since Angular4
2- Although this was not the main issue I got CORS error shown on chrome (probably after using httpclient)
3- there was no need to add responsType, After solving the cors error on server side it worked.
Old wrong code:
var headers = new Headers();
headers.append("Accept", 'application/json');
headers.append('Content-Type', 'application/x-www-form-urlencoded');
let options = new RequestOptions({ headers: headers });
let postParams = "grant_type=password&username="+username+"&password="+password;
this.http.post("api/Token", postParams, options)
.subscribe(data => {
console.log(data);
},
error => {
//can I get the response body here?
console.log(JSON.stringify(error));
Related issue is found on this link https://github.com/angular/angular/issues/19555 so I tried to add responseType, as follow
Also According to this question , I tried to pass the responseType property, but it is not working either Angular 6: How to set response type as text while making http call
this.http.post(AppSettings.apiaddress + "Token", postParams, {headers,responseType: ResponseContentType.Text})
If noway to get it working, is there is a way to get the data on the error handler as workaround?
Appreciate your help but If you don't know answer no need to downvote, so others can help, thanks