Change :
addGroupe(groupe: Groupe): Observable<Groupe> {
return this.http
.post<Groupe>(`${this.url}/groupe`, groupe)
.pipe(catchError(this.handleError('addGroupe', groupe)))
}
To :
let options = {
headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
};
addGroupe(groupe: Groupe): Observable<Groupe> {
return this.http
.post<Groupe>(`${this.url}/groupe`, groupe, options)
.pipe(catchError(this.handleError('addGroupe', groupe)))
}
Additional:
to make sure all browsers support it I recommend using polyfill
import 'url-search-params-polyfill';
npm i url-search-params-polyfill --save
More explicitly :
CORS errors can happen for several reasons but always about security risk, the server have the right to determine the access the client has, and the browsers followers its instructions that are a part of the response headers
, like:
'Access-Control-Allow-Credentials' : true,
'Access-Control-Allow-Origin':'*',
'Access-Control-Allow-Methods':'GET',
'Access-Control-Allow-Headers':'application/json'
You issue if more likely as I suggested earlier (application/x-www-form-urlencoded
). However in the off chance that didn't work you can run curl -I http://ip:port
in terminal (or cmd but need to be installed if you're using windows) to see if the server Access-Control-Allow-Origin
is allowing your client. Either by specifying the IP:PORT or *
(means that all are allowed). If you're not allowed then you can either change the response headers in the server (if you can access to) or use a CORS proxy
(if the server is on the WAN not just locally stated), or use a browser plugin to ignore the server instructions but I wouldn't recommend that because other users would be able to use it.