简体   繁体   English

metamask web3未定义

[英]metamask web3 is undefined

I have build an eth node in local PC. 我已经在本地PC中建立了一个eth节点。 The code is no problem. 代码没问题。 But when I exited from eth node, and use metamask to run the code,web3 is undefined. 但是,当我从eth节点退出并使用metamask运行代码时,web3是未定义的。

Can you tell me how to solute the problem? 你能告诉我如何解决这个问题吗?

<!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>

Are you trying to serve from file:// ? 您是否要通过file://提供服务? If so, it will not work. 如果是这样,它将无法正常工作。 MetaMask refuses to serve filesystem URLs. MetaMask拒绝提供文件系统URL。 Serve your application via a web server. 通过Web服务器服务您的应用程序。 A one-liner such as python -m SimpleHTTPServer 8000 will do. python -m SimpleHTTPServer 8000这样的单行代码就可以。

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

window.web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM