2

I need to make a CORS request from localhost. I have the Allow-Control-Allow-Origin Chrome plugin installed and turned on and when I hover over the menu item it shows: "Allow-Control-Allow-Origin: *". I created a .bat file with the following contents:

start chrome http://localhost:8080/MyPage.html --disable-web-security --user-data-di

This opens a new Chrome instance at the expected url but the CORS request still fails with the following error:

Failed to load [rest-url]: 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:8080' is therefore not allowed access.

Can you recommend any workarounds for this?

user8570495
  • 1,397
  • 3
  • 13
  • 25
  • Is this just for debugging? Also is the remote HTTP or HTTPS? – Patrick Roberts Dec 19 '17 at 08:49
  • I'm calling an API to a trading platform. The remote is https. I was planning on making API calls from localhost on my local machine since I'll be the only user. I'm using http-server npm package to host an html page on my local filesystem. – user8570495 Dec 19 '17 at 08:56

1 Answers1

0

could you show the code and give us some more information about the technology etc. which you are using?

It seems like you´re not sending a "preflight"-Request, your preflight-request does not contain the needed headers or your remote does not return the right headers:

For example, suppose the browser makes a request with the following headers:

Origin: http://yourdomain.com
Access-Control-Request-Method: POST
Access-Control-Request-Headers: X-Custom-Header

Your server should then respond with the following headers:

Access-Control-Allow-Origin: http://yourdomain.com
Access-Control-Allow-Methods: GET, POST
Access-Control-Allow-Headers: X-Custom-Header

Credits to: @monsur

See also: CORS - How do 'preflight' an httprequest?

the Access-Control-Request-Headers needs to contain all custom headers your sending allong with the real request, it will fail if not. At least for me, the error messages are somewhat cryptic and don´t always point in the right direction.

Salfii
  • 47
  • 9