1

**Hi, I am doing a sample project on Ethereum where 2 fields ( Emp name, Emp Id) will be taken from the UI and set those values using a smart contract and show the values back to UI. Getting below 2 errors.Please help me out to solve this.

  1. Failed to load http://localhost:8545/: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'null' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute. t.send @ web3.min.js:1

  2. Uncaught Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545. at Object.InvalidConnection (web3.min.js:1) at t.send (web3.min.js:1) at n.send (web3.min.js:1) at l.accounts (web3.min.js:1) at index.html:41 InvalidConnection @ web3.min.js:1 t.send @ web3.min.js:1 n.send @ web3.min.js:1 (anonymous) @ web3.min.js:1 (anonymous) @ index.html:41

0

2 Answers 2

0

You need to allow remote connections to ethereum node. I am assuming you are trying to connect to remote ethereum node. To allow ethereum node a connection requests from other devices on which geth is not running you'll need to use --rpcaddr and --rpccorsdomain flags to allow external RPC connections.

Use the following command for initiating geth

geth --datadir "path to data directory" --networkid 9876 --rpcport "8545" --rpcaddr "0.0.0.0" --rpccorsdomain "*" 
2
  • path to data directory means? Commented Sep 24, 2018 at 3:23
  • --datadir to specify the directory for private ethereum blockchain. This is good practice when you are interacting with different chains. Commented Sep 24, 2018 at 4:54
0

Make sure the Ethereum node is running: Launch Ganache, Hardhat, or Geth on port 8545. Use the script below to verify its reachability, which will give a JavaScript Object Notation (JSON) response.

curl http://127.0.0.1:8545 

Step 2: Activate CORS on the node. If using the Ganache Command Line Interface, type:

 ganache-cli --host 127.0.0.1 --port 8545 --cors="*" 

Else, leverage going to "Settings → Server → CORS domain" and set it to *.

Step 3: Using a local HTTP server, serve your HTML, as browsers usually block file:// requests due to CORS.

Then, launch a sample server using the script below before opening your app at http://localhost:8000/index.html:

python -m http.server 8000 

Step 4: Do the initialization of Web3 with correctness, while using "127.0.0.1 instead of localhost to prevent potential DNS worries":

if (typeof window.ethereum !== 'undefined') { web3 = new Web3(window.ethereum); await window.ethereum.enable(); 

} else { web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545")); }

Step 4: Avoid the unneeded credentials mode

Ensure withCredentials is disabled except otherwise, as this can spawn stricter CORS checks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.