0

I have a pretty simple Angular 6 service that calls a Spring Boot service. Here is how it looks like in Angular :

getData(user, numberOfRequests, graphDepth, max): Observable<String> {
    var headers = new Headers();
    var body = {    "user" : user,
                    "numberofRequests" : numberOfRequests,
                    "graphDepth" : graphDepth,
                    "max" : max };
    headers.append('Content-Type', 'application/x-www-form-urlencoded');    
    return this.http.post<String>('http://XX.XX.XXX.XXX:8083/collect', body, httpOptions);
}

Here is the server side :

@CrossOrigin(origins = "*")
    @RequestMapping(value = "/collect", method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
    @ResponseBody
    public String startServices(@RequestBody UserParameters parameters) {
        LOGGER.info("Calling for data");
        String jsonOutput = collect.getData(parameters);
        LOGGER.info("Data ready to be served");
        return jsonOutput;
    }

It works well when both application run locally or when they run on 2 different VMs. But when I deploy on a cloud service I get something that seems to be a CORS error :

 Reason: CORS request did not succeed

error: error { target: XMLHttpRequest, isTrusted: true, lengthComputable: false, … }
headers: Object { normalizedNames: Map(0), lazyUpdate: null, headers: Map(0) }
 message: "Http failure response for (unknown url): 0 Unknown Error"
 name: "HttpErrorResponse"
 ok: false
 status: 0
 statusText: "Unknown Error"
 url: null

This error happens when the webapp and the backend run on the same VM or in two separate VM with 2 different IP...

Do you know what could cause that problem ? How can it be a CORS error ? And do you know how can I try to fix it ?

Thanks!

1 Answer 1

1

try:

httpOptions = {
    headers : new HttpHeaders({'Content-Type', 'application/x-www-form-urlencoded'})
};

return this.http.post<String>('http://XX.XX.XXX.XXX:8083/collect', body, httpOptions);

How do you link the headers to the httpOptions?

For me that works.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually I had this on my class : const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'my-auth-token' }) }; Your modification bring the same issue...
Oh ok. You could also try some more changes: In the Security configuration class (extends WebSecurityConfigurerAdapter), in the method configure(HttSecurity httpSecurity) try this: http.cors().disable(); maybe even http.csrf().disable(); I hope this helps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.