I am new to angular and facing some CORS problem. I tried all the possible solution including setting up CORS header and the server side change. But still not working.
I changed the server end with the header 'Access-Control-Allow-Origin': '*' in response. I tried with postman and it's working fine and giving the header response. I also set the header in my Angular app while sending the request. But still giving the error. This is the giving:
Access to XMLHttpRequest at 'http://skewcommerce.test/api/user/add/' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.
This is my service to send the request.
import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {HttpHeaders} from '@angular/common/http';
import {environment} from '../../environments/environment';
@Injectable({
providedIn: 'root'
})
export class ApiService {
baseUrl = environment.APIUrl;
token: any;
constructor(private http: HttpClient) {
}
postData(url, data) {
const httpOptions = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
});
return this.http.post(this.baseUrl + url + '/', data, {headers : httpOptions});
}
}
Please help me to know what I did wrong. :(