-1

I am running two applications on my local desktop.

One is built in java which serves as the API, the other one is built in Angular which is the front-end application.

I am trying to access the backend data from Angular and I am running into an error :

Access to XMLHttpRequest at 'http://localhost:8080' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 public getMethod(): Observable<Availability[]> {
    return this.http
    .get(API_URL)
      .map(response => {

      })
      .catch(this.handleError);
  }

I am doing something like this in Angular to grab the data, and the error keeps coming up.

My question is, is this an angular error, or a Backend error(Java)?

mpromonet
  • 11,326
  • 43
  • 62
  • 91
Abby
  • 67
  • 1
  • 11

3 Answers3

1

That's a backend error. You have to allow cors at your java backend. Check this

Máté Antal
  • 157
  • 1
  • 21
0

An easy way to make http request in local is install a cors extension in your browser

As example, if you use Google Chrome install it:

Allow-Control-Allow-Origin Extension

So, enable the cors clicking in the button and make a request :)

0

Create a proxy.conf.json in the root of your project next to package.json.

Here's an example.

{
    "/service/*": {
        "target": "http://localhost:8080",
        "changeOrigin": true
    }
}

Add your proxy.conf.json to serve options in angular.json. As in:

 "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "app:build",
            "proxyConfig": "proxy.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "app:build:production"
            }
          }
        }

That's it. Just ng serve as you usually do and you can start proxying to your server. Make sure you don't manually offer host url in your http calls. Just request from /service.

Anjil Dhamala
  • 1,544
  • 3
  • 18
  • 37