0

I've written a small SPA app using Angular 2 on the frontend & NodeJS for the backend

I started both backend & frontend on my computer (http://localhost:3000 & http://localhost:4200)

When Angular tries to get data from backend rises an error:

XMLHttpRequest cannot load http://localhost:3000/mailboxes. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access.

EXCEPTION: Uncaught (in promise): 0 - {"isTrusted":true} ...

How to make this work?

1
  • 1
    read this Commented Feb 22, 2017 at 9:03

1 Answer 1

1

Your service and client are running on 2 different ports so as far as you service is concerned, this request is coming form a foreign location. So you need to let your service side know that you are allowing this request.

To do this you need to enable cross origin resource sharing on your service https://en.wikipedia.org/wiki/Cross-origin_resource_sharing

Here is the link to an npm cors library very easy to use https://www.google.co.za/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=npm+cors

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

1 Comment

It helped. Thanks a lot.

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.