I'm using angular 6, httpClient, trying to login, no token required, just a username and a password, can't modify the server.
login.ts
onLogin() {
this.authService.login(this.email, this.password).subscribe(data => {
console.log(data);
});
}
auth service
login(username, password) {
const data = {
username: username,
password: password
};
const headers = new HttpHeaders();
headers.set('Content-Type', 'application/json');
return this.http.post(this.login_url, data, { headers: headers });
}
There are plenty of questions like this one but those were for angularjs and I'm using angular 6, some answers gave articles to read with no practical coding answer, and some says to change the header, but as far as I can tell from my postman tests, the server only accepts 'Content-Type', 'application/json'
And it's working perfectly on postman. When I do it in angular, my post request, becomes options.
Some even say that to avoid CORS, you should setup an interceptor, but that's for when you have a token, no? you save it in localStorage and then you use it, I don't have a token. The server X-Session
in the header.
Some say, to create a proxy, package.json
,
`"start": "ng serve --proxy-config proxyconfig.json",`
proxyconfig.json
{
"/api": {
"target": "example.com",
"secure": false,
"changeOrigin": true
}
}
And the login_url
in auth service
would point to the rest endpoint without /example/api
. I also tried that, it's not working either.
Some says, you have to edit the webserver, to add Access-Control-Allow-Origin
, I'd like not to do that, if possible. If not, I'll contact my company and ask them to do it. I'd like a solution using angular 6.