6

I've seen other similar issues here on Stack Overflow but so far nothings quite done anything for me yet.

I have an angular app serving to http://localhost:4200. I have a backend at http://localhost:8080/myApp/

I'm trying to set reach the backend from my frontend with the following call:

public getPeople(): Observable<PeopleModel[]> {
    return this.http
        .post(API_URL + '/getPeopleDetails', {})
        .map(response => {
            const people = response.json();
            console.log(people);
            return partners.map((people) => new PeopleModel(people));
        })
        .catch(this.handleError);
}

the API_URL is set to http://localhost:8080/myApp

My proxy config looks like the following:

{
    "/myApp/*": {
        "target": "http://localhost:8080/myApp",
        "secure": false,
        "changeOrigin": true,
        "logLevel": "debug",
        "pathRewrite" : {"^/myApp" : "http://localhost:8080/myApp"}
    }
}

My package.json has the following:

"start": "ng serve --proxy-conf proxy.conf.json",

The error I'm receiving when trying to run the app is a CORS one and a 403.

Failed to load http://localhost:8080/myApp/getPeopleDetails: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. The response had HTTP status code 403.

I'd appreciate any help. Thanks!

2
  • What is your 'backend'? Commented Nov 28, 2017 at 16:35
  • 1
    A spring boot app that has a several endpoints defined Commented Nov 28, 2017 at 17:00

4 Answers 4

3

At the root of your application parallel to package.json file, create a new file
proxy.config.json

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

Now in your package.json in scripts: {}, add the following flax with file name proxy.config.json to start"

{
"name":"xyz",
"version":"1.0.0",
"license":"MIT",
"angular-cli": {},
"scripts": {
    "start":"ng serve --proxy-config proxy.config.json",
    "test":"ng test"
}

}

Hope this works for you.

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

4 Comments

Unfortunately, I already have all of this so it doesn't really help with trying to get the proxy to work.
Are you behind a corporate firewall in any case?
Give me time till tomorrow, I too dealt with this long back. Let me have a look at my configs, ll get back to you.
@RunFranks525 I just checked. I have just got a file at the root .typingsrc and have as { "rejectUnauthorized": false, "registryURL": "api.typings.org", "httpProxy": "http proxy", "httpsProxy": "https proxy", } This works for me. Also put the proxy in .npmrc file.
0

It looks like the issue is that the option should be "--proxy-config", not "--proxy-conf" in "start": "ng serve --proxy-conf proxy.conf.json"

Comments

0

You may need to pass backend url without "api". Following another tip on proxy.conf.json:

{
  "/api/*": {
    "target":"http://localhost:3000",
    "pathRewrite": {
      "^/api": "/myApp"
    },
    "secure":false
  }
}

https://angular.io/guide/build#proxying-to-a-backend-server

Comments

0

I saw guys give you advice how to set up the proxy, actually in your case after setting up the proxy, please set your API_URL to be http://localhost:4200/api, it's the proxy who will turn it into http://localhost:8080/myApp, not you, that's why you got CORS problem again, because you've already set up the proxy but actually you haven't used it with a wrong API_URL,that's it. Good luck Bro!

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.