0
votes

I am using Geth 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 dont know where is the problem..is there any problem related to flags in geth node or some method within Js? Thanks

1
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
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.jsHsn
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

1 Answers

1
votes

I faced the same issue and I fixed it after I run the following command.

npm install [email protected] --save

check this link