I am using a Go-Ethereum node with flags
geth --networkid '49' --datadir 'E:\Dir' --rpc --rpcapi 'web3, net, personal, admin, eth' --rpccorsdomain '*' console
I am trying to get Accounts using Web3.js. Whenever I try to request accounts using..
web3.eth.accounts[0]
or
web3.eth.getAccounts(accounts => console.log(accounts));
i get an error and hence when print the whole Web3 response there isnt any accounts there and it shows the following error..
[Exception: Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545. at Object.InvalidConnection (http://127.0.0.1:8080/node_modules/web3/dist/web3.js:3137:16) at HttpProvider.send (http://127.0.0.1:8080/node_modules/web3/dist/web3.js:4350:18) at RequestManager.send (http://127.0.0.1:8080/node_modules/web3/dist/web3.js:6357:32) at Eth.get [as accounts] (http://127.0.0.1:8080/node_modules/web3/dist/web3.js:6260:62) at Eth.remoteFunction (:2:14)]
and when i try my index.html page without an HTPP-SERVER running the error is
Failed to load http://localhost:8545/: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Credentials' header in the response is '' which must be 'true' 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. HttpProvider.send @ web3.js:4348
Uncaught Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545. at Object.InvalidConnection (web3.js:3137) at HttpProvider.send (web3.js:4350) at RequestManager.send (web3.js:6357) at Eth.get [as accounts] (web3.js:6260) at index.html:50
I don't know where the problem lies. Is there any problem related to flags in the node or some method within JavaScript?
I faced the same issue and I fixed it after I run the following command.
npm install web3@0.20.6 --save
check this link