Reputation: 565
I am trying to make a third party authorisation. When I hit a POST request to server, it sends HTML as response.
Header
private httpOptions = {
headers: new HttpHeaders({
'Accept': 'text/html',
'Content-Type': 'application/json'
}),
responseType: 'text',
};
API Call
return this.http.post(this.authUrl, this.httpOptions)
.subscribe(data => {
console.log(data);
});
I am getting a SyntaxError:
Unexpected token < in JSON at position 0 at JSON.parse
Upvotes: 1
Views: 9295
Reputation: 191
In my opinion the best way to put your response type is when you are calling the http post this will stopped the HttpClient from trying to parse the empty response as a JSON object.
this.http.put(
url,
data,
{
headers: new HttpHeaders().set('Content-Type', 'application/json'),
responseType: 'text'
}
).subscribe( .... );
with post
return this.http.post(url, params, {headers : headers, withCredentials : true,responseType: 'text'});
Upvotes: 1
Reputation: 1499
private httpOptions = {
headers: new HttpHeaders({
'Accept': 'text/html',
'Content-Type': 'application/json'
}),
responseType: 'text'
};
Your HTTP options had an extra comma after responseType:'Text'
return this.http.post(this.authUrl,null, this.httpOptions)
.subscribe(data => {
console.log(data);
});
Also, HTTP POST for HttpClientModule takes in payload as second argument within the POST call.
Upvotes: 5