I've seen a lot of discussions on this topic, but none have addressed my specific issue.
Currently, I am working on an angular 5 application and trying to retrieve an authentication token by sending a post request to a server. Testing the connection using Postman shows no issues. However, when attempting it in Angular, I encounter the following error:
Error Screenshot
Below is the code snippet I have written so far:
getAccessToken2() {
let url = "http://api.example.com/oauth/token";
var headers = new HttpHeaders();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
headers.append('Accept', 'application/json');
let urlSearchParams = new URLSearchParams();
urlSearchParams.set('grant_type', 'password');
urlSearchParams.set('username', '<email>');
urlSearchParams.set('password', '1234');
urlSearchParams.set('client_id', '2');
urlSearchParams.set('client_secret', 'secret');
urlSearchParams.set('scope', '*');
let body = urlSearchParams.toString();
return this.http.post(url, body)
.subscribe((data: any) => {
console.log(data);
});
}
Additionally, here's a successful Postman request screenshot for reference: Postman Screenshot
I also encountered the Access-Control-Allow-Origin issue and temporarily fixed it by using a Chrome add-on called "CORS". Is there a more permanent and effective solution for this problem?
Appreciate any advice in advance.