I am since yesterday trying to enable cors in php to consume the api from angular. I'm using Slim and the tuupola / cors-middleware library, I followed the instructions, but it does not work for me, I have not been able to make a post and I already tried different ways.
This is my code: Slim middleware config:
$app->add(new Tuupola\Middleware\CorsMiddleware([
"origin" => ["*"],
"methods" => ["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"],
"headers.allow" => ["Authorization", "If-Match", "If-Unmodified-Since"],
"headers.expose" => ["Etag"],
"credentials" => false,
"cache" => 0
]));
Slim controller response:
return $response->withJson(
[
'success' => [
'message' => $signInUserResponse->message()
]
],
200
);
Angular 6 call code:
export class UserService {
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'Application/json',
'Access-Control-Allow-Origin':'*'
})
};
selectedUser: User;
readonly URL_API = 'http://localhost:8000/signup';
users: User[];
constructor(
private http: HttpClient,
){
this.selectedUser = new User;
}
postUser(user: User){
return this.http.post(this.URL_API, user, this.httpOptions);
}
getUser(){
return this.http.get(this.URL_API);
}
}
This is the error:
Access to XMLHttpRequest at 'http://localhost:8000/signup' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.