near-api-js icon indicating copy to clipboard operation
near-api-js copied to clipboard

Access to fetch at 'https://rpc.testnet.near.org/' from origin 'http://localhost:3000' has been blocked by CORS policy

Open masafumimori opened this issue 2 years ago • 1 comments

Describe the bug When I try to call method with connecting to testnet from localhost:3000, the CORS error occurs.

Access to fetch at 'https://rpc.testnet.near.org/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Do I need to run the node locally using Docker to test interaction with smart contracts?

To Reproduce Steps to reproduce the behavior:

  1. Go to 'my repo'
  2. Clone the project
  3. Move to near-wasm directory
  4. yarn install & yarn dev
  5. Go to [http://localhost:3000](http://localhost:3000)([http://localhost:3000)([http://localhost:3000](http://localhost:3000](http://localhost:3000)([http://localhost:3000](http://localhost:3000)))
  6. click Login button displayed and press + button

Expected behavior Can call method on testnet.

Desktop (please complete the following information):

  • OS: MacOS M1Pro
  • Version Monetary v12.0

masafumimori avatar Jul 15 '22 01:07 masafumimori

I created local dev environment following this. But I still get the same CORS policy issue.

Access to fetch at 'http://127.0.0.1:8332/' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

So, might be because of Vite.js I am using, instead of near-api-js.

masafumimori avatar Jul 15 '22 04:07 masafumimori

@masafumimori The reason you are getting CORS errors is because you are adding a non-supported header to the requests sent to the RPC endpoint. The browser will block any requests which contain headers which are not present in the access-control-allowed-headers response header.

headers is currently required in the connect configuration, but you can set it to an empty object to work around this.

morgsmccauley avatar Aug 23 '22 01:08 morgsmccauley

Thank you @morgsmccauley Closing the issue.

marcinbodnar avatar Aug 23 '22 14:08 marcinbodnar