0

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Hsn
  • 1,168
  • 2
  • 16
  • 39
  • Maybe a silly question: are geth and your javascript running on the same host machine? At first glance it looks like a basic connection failure. Also, any errors in your geth logs? – carver Aug 20 '18 at 19:04
  • Yeah, it is running on the same machine, i am using the latest beta version and having problem in that version otherwise it is running fine on older versions of web3.js – Hsn Aug 21 '18 at 09:48
  • another solution is to add this line of code var options = {timeout: 20000,headers: [{name: 'Access-Control-Allow-Origin', value: '*'}]}; with http provider like web3 = new Web3(new Web3.providers.HttpProvider("", options)); – Hsn Aug 21 '18 at 09:48

1 Answers1

1

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

bereket gebredingle
  • 12,064
  • 3
  • 36
  • 47