3

我已经在本地 PC 中构建了一个 eth 节点。代码没有问题。但是当我从 eth 节点退出并使用元掩码运行代码时,web3 未定义。

你能告诉我如何解决这个问题吗?

<!DOCTYPE html>
<html>
<head>
<title>Using web3 API with MetaMask</title>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js/dist/web3.min.js"></script>
<!-- for ecrecover -->
<script type="text/javascript" src="D:\blockchain\test\ethereum\ethereumjs-util.js"></script>
<script>
window.addEventListener('load', function() {
  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    console.log('No web3? You should consider trying MetaMask!')
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
  }
</script>
</head>
</html>
4

2 回答 2

2

你想从 服务file://吗?如果是这样,它将无法正常工作。MetaMask 拒绝提供文件系统 URL。通过 Web 服务器为您的应用程序提供服务。像python -m SimpleHTTPServer 8000这样的单线。

于 2018-10-07T21:46:13.907 回答
0

当您的以太坊节点关闭时,您的网站无法从您的节点获取信息,您可以切换到 infura.io 提供商

window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io"))
于 2018-10-08T15:44:21.767 回答