1

I start my local node using the following command:

geth --datadir  "D:\EthereumLocalNode" --networkid 1114 console 2>>  "D:\EthereumLocalNode\myLog.log" --rpc --rpccorsdomain "*" --nodiscover --nodekey boot.key

and until 2 weeks ago, I could successfully connect and query my blockchain in chrome browser using :

web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
abi = JSON.parse('.....myContract abi here.....');
myContract = web3.eth.contract(abi);
contractInstance = myContract.at('...My Contract address in bc...');

Now I'm getting the following error in developer tool of chrome :

Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header has a value 'null' that is not equal to the supplied origin. Origin 'null' is therefore not allowed access.

However I can still connect to this node and send transactions using remix, or with a js script that I run with nodejs that can make transactions to exactly same node still using web3 , which in the first line starts with:

Web3 = require('web3');
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

and this works fine.

So I'm guessing something has changed with the way Chrome works since the last one or two updates.

There are people complaining about Chrome's latest updates about something has changed with CORS. Anyways, I dont know what to do with CORS or how to change its settings.

Do I need to:

  • change the way I start my node?
  • change something in Chrome settings?
  • or add some lines to my javascript code I use in my html.

Would be glad if someone helped me here.

0

The preflight error is related to CORS as you said.

I fixed this error by responding the the OPTIONS request with a 200 code while setting the headers to correct form .

in my Express App i used this :

router.route("/*").options(function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, X-Auth");
    res.header("Access-Control-Allow-Methods", "GET, PUT, POST,DELETE");
    res.status(200).send();
});

where :

var express = require('express');
var router = express.Router();

It is not really a good idea to do what i did if you are in a prod enviroment.

Or you can install the allow control allow origin chrome extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.