Good,
I'm a bit desperate because I am unable to perform an UPDATE through an API. The request gives me an error:
403 for Invalid CORS request.
My service:
private urlApi= 'http://localhost:8080/api';
updateIt(id: string, lang: string, body: Object) {
let bodyString = JSON.stringify(body);
let headers = new Headers({ 'Accept': '*/*' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.copiesUrl + "/" + id + "/lang/" + lang, body, options)
.map((res:Response) => res.json())
.subscribe(
data => console.log('Success update ', data),
error => console.error('Error: ${error}')
);
}
I have tried to put a chrome extension for the CORS theme and in the "Intercepted URLs or URL patterns" field I have placed*://*/*
Note that to make query requests I am not having any problem.
Well I'm new to SO but I think that if I have found the solution I should write an answer to my own question to leave the solution to the problem to other users.
Finally, and even though I have triggered another error, I have managed to solve the CORS issue by touching the back part of my service. That is to say in the API. In my case the back is developed in Java and Spring. Simply adding the Spring annotation
@CrossOrigin
fixes it.So that the service is finally like this:
I think your problem may be with the content you are sending. If you use
JSON.stringify(body)
I understand that it is generating a Json that is what you are sending in the request. Therefore, theContent-Type
should beapplication/json
.Try this to see if it fixes it.
I think this should go as a comment but I don't have the 50 reputation points needed to comment so I'm putting it here.
I have read your other problem and I think you could use a tool that I use daily to be able to test my own and third-party APIs. It's called Postman and it allows you to perform all kinds of tests with APIs, very useful when you have to investigate and test how they are or to test what you're doing.
I hope I can serve.