#CORS Error

14 messages · Page 1 of 1 (latest)

spark cove
#

Hey guys :)
I'm trying to build a web application for uni project and im stuck at a CORS error.

My setup is Spring Boot for Backend. My current Enpoint is /api.

And in typescript I use httpClient.get just to have a basic request.

I have been stuck at this problem for quite a while.
Would be very grateful if anyone can give a helping hand or guide me on how to do it.

#
Java Controller (Endpoint)

@CrossOrigin(origins = "http://localhost:4200")
@RestController
public class GroupMakerController {
    @GetMapping("/api")
    public String button() {
        System.out.println("have I been called?");
        return "This is from backend";
    }
}

proxy.conf.json

{
  "/api": {
    "target": "http://localhost:8080",
    "secure": false
  }
}

component.ts

magic() {
    this.httpClient.get('http://localhost:8080/api', {responseType: 'text'})
      .subscribe(resp => {
        alert(resp.toString());
      });
  }```
tame citrus
#

did you make a corsconfig ?

fallen shore
#

CORS should be configured in the backend, you shouldn't need any setup for it on the angular side

spark cove
#

Okay I understand, thank you guys :)
Is the Annotation '@CrossOrigin' not enough?
How else should I configure it in the backend?

fallen shore
sharp trellis
spark cove
#

this is my angular.json

tame citrus
#

it would look something like this in java

kind fossil
#

That should be needed only if using SpringSecurity

sharp trellis
fluid relic
#

Hi @spark cove
You need to define your cors policy in the backend, in application config file of your spring boot project