near-api-js
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
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:
- Go to 'my repo'
- Clone the project
- Move to
near-wasm
directory - yarn install & yarn dev
- 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)))
- 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
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 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.
Thank you @morgsmccauley Closing the issue.