Skip to content

CORS Request Issues

Atul edited this page Apr 29, 2016 · 1 revision

I ran into this problem so thought it would good to document how I fixed it.

Scenario: Can't connect from javascript

When you make a request in a browser to an endpoint it works. When you make the request through javascript it doesn't and you get the error:

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

Solution:
add the following to your node script

server.use(restify.CORS());

Scenario: Can't get content-range header from javascript

When you check with developer tools you see Content-Range header but not in your javascript response. I ran into this issue with Angular2.

Solution:

server.use(restify.CORS({
credentials: true,
headers: ['Content-Range']}));

Basically, any add missing headers to the list.