簡體   English   中英

metamask web3未定義

[英]metamask web3 is undefined

我已經在本地PC中建立了一個eth節點。 代碼沒問題。 但是,當我從eth節點退出並使用metamask運行代碼時,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>

您是否要通過file://提供服務? 如果是這樣,它將無法正常工作。 MetaMask拒絕提供文件系統URL。 通過Web服務器服務您的應用程序。 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