首页 > 解决方案 > Web3.js 不使用 geth 节点返还帐户

问题描述

我正在使用带有标志的 Geth 节点

geth --networkid '49' --datadir 'E:\Dir' --rpc --rpcapi 'web3, net, personal, admin, eth' --rpccorsdomain '*' 控制台

我正在尝试使用 Web3.js 获取帐户。每当我尝试使用..请求帐户时

web3.eth.accounts[0]

或者

web3.eth.getAccounts(accounts => console.log(accounts));

我收到一个错误,因此当打印整个 Web3 响应时,那里没有任何帐户,它显示以下错误..

[异常:错误:连接错误:无法连接到节点 http://localhost:8545。在Object.InvalidConnection ( http://127.0.0.1:8080/node_modules/web3/dist/web3.js:3137:16 ) 在 HttpProvider.send ( http://127.0.0.1:8080/node_modules/web3/dist/ web3.js:4350:18 ) 在 RequestManager.send ( http://127.0.0.1:8080/node_modules/web3/dist/web3.js:6357:32 ) 在 Eth.get [作为帐户] ( http:// 127.0.0.1:8080/node_modules/web3/dist/web3.js:6260:62 ) 在 Eth.remoteFunction (:2:14)]

当我在没有运行 HTPP-SERVER 的情况下尝试我的 index.html 页面时,错误是

加载http://localhost:8545/失败:对预检请求的响应未通过访问控制检查:响应中的“Access-Control-Allow-Credentials”标头的值为“”,必须为“真”当请求的凭据模式为“包含”时。因此,Origin 'null' 不允许访问。XMLHttpRequest 发起的请求的凭证模式由 withCredentials 属性控制。HttpProvider.send@web3.js:4348

未捕获的错误:连接错误:无法连接到节点 http://localhost:8545。在 Object.InvalidConnection (web3.js:3137) 在 HttpProvider.send (web3.js:4350) 在 RequestManager.send (web3.js:6357) 在 Eth.get [作为帐户] (web3.js:6260) 在索引.html:50

我不知道问题出在哪里..是否有与 geth 节点中的标志或 Js 中的某些方法相关的问题?谢谢

标签: ethereumweb3web3jsgo-ethereumgeth

解决方案


我遇到了同样的问题,并在运行以下命令后修复了它。

npm install web3@0.20.6 --save

检查这个链接


推荐阅读