Solved by using NelmioCorsBundle.
I'm using angular 4.3.3 for frontend and Symfony3 for backend, and I use FosRestBundle also.
In Angular, and I'm working with HttpClient to send or request some data.
Requesting data is working good and I can display the results but the problem is when I try to send data I get this cors error
On the other hand I can post json data using postman
This is the error:
1:1 XMLHttpRequest cannot load
http://127.0.0.1/rest/web/app_dev.php/api/comments/new/1. Response for preflight
has invalid HTTP status code 405
Error occured.
{"content":"test comment","email":"test@gmail.com"}
and this is the code typeScript:
import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http';
constructor(private http: HttpClient, private globals: Globals, private activatedRoute: ActivatedRoute) {
}
postComment(post) {
const body = JSON.stringify(post);
this.http.put(this.globals.baseUrl + 'comments/new/' + this.id, body).subscribe(
res => {
console.log(res);
},
err => {
console.log('Error occured.');
console.log(body)
}
);
}
In th symfony Project: web/.htaccess
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
See Question&Answers more detail:os