Invalid CORS request Angular 2

0

Good morning,

I'm a little desperate because I'm unable to perform an UPDATE through an API. The request gives me an error:

  

403 by 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 *://*/*

Highlight that to make requests for consultation I'm not having any problem.

    
asked by Findelias 28.02.2017 в 11:27
source

3 answers

3

Well, I'm new to SO but I think that if I have found the solution I have to write an answer to my own question to leave the other users the solution to the problem.

Finally, and even though I have unleashed another error, I managed to solve the issue of the CORS by touching the back of my service. That is, in the API. In my case, the back is developed in Java and Spring. Simply adding the annotation of Spring @CrossOrigin is solved.

So the service is finally like this:

 private urlApi= 'http://localhost:8080/api';    
updateIt(id: string, lang: string, body: Object) {
        let bodyString = JSON.stringify(body);
        let headers = new Headers({ 'Content-Type': 'application/json' });
        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}')
                         );
    }
    
answered by 02.03.2017 / 10:23
source
0

I think your problem may be with the content you are sending. If you use JSON.stringify(body) I understand that you are generating a Json that is what you are sending in the request. Therefore, the Content-Type should be application/json .

Try this to see if it is solved.

    
answered by 02.03.2017 в 11:46
0

I think this should go as a comment but I do not have the 50 necessary reputation points to comment on, so I put it here.

I have read your another problem and I think you could use a tool that I use daily to test your own and third-party APIs. It's called Postman and it allows you to do all kinds of tests with APIs, very useful when you have to investigate and prove as they are or to test the one that you're doing.

I hope I can help you.

    
answered by 07.03.2017 в 09:46