【问题标题】:Getting error while connecting to smart contract using web 3使用 web 3 连接到智能合约时出错
【发布时间】:2018-11-30 14:06:05
【问题描述】:

我推荐https://medium.com/@amkurian/interacting-with-ethereum-smart-contracts-through-web3-js-e0efad17977连接到智能合约,我想简单地访问智能合约的get()方法。

代码如下:

    <!DOCTYPE html>
    <html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Web 3</title>
        <link rel="stylesheet" type="text/css" href="index.css">
        <script src="./node_modules/web3/dist/web3.min.js"></script>
    </head>

    <body>
        <div class="container">
            <h1>First Smart Contract</h1>
            <h2 id="instructor"></h2>
            <label for="name" class="col-lg-2 control-label">Instructor Name</label>
            <input id="name" type="text">
            <label for="name" class="col-lg-2 control-label">Instructor Age</label>
            <input id="age" type="text">
            <button id="button">Update Instructor</button>
        </div>
        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js "></script>

        <script>
            if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
            }
            else {
                // set the provider you want from Web3.providers
                web3 = new Web3(new Web3.providers.HttpProvider("http://my_ip_address:8545"));
            }

            web3.eth.defaultAccount = 'account address';

            var CoursesContract = web3.eth.contract(my abi);

            var Courses = CoursesContract.at('my smart contract address');

            console.log(Courses.getName());

        </script>

    </body>

    </html>

But i m getting the below error in the console:
web3.min.js:1 Uncaught Error: CONNECTION ERROR: Couldn't connect to node 

    http://my ip here:8545.
        at Object.InvalidConnection (web3.min.js:1)
        at i.send (web3.min.js:1)
        at s.send (web3.min.js:1)
        at _.e [as call] (web3.min.js:1)
        at c.call (web3.min.js:1)
        at c.execute (web3.min.js:1)
        at index.html:40

请帮忙

【问题讨论】:

    标签: node.js blockchain smartcontracts web3


    【解决方案1】:

    编辑答案:

    运行命令 ganache-cli 并让它继续运行。 将http://my_ip_address:8545 替换为http://localhost:8545/

    这将解决您的问题。

    以前删除的答案:

    问题可能是您的 ganache 本地节点未正确配置或未运行。你能在命令行中显示运行 ganache-cli 的输出吗?

    而且,如果您有“my_ip_address”,您可以简单地将其替换为http://localhost:8545/

    您是否也可以在浏览中转到http://localhost:8545 如果它有效,您应该会看到“400 Bad Request”。如果它不起作用,您将看到无法访问站点。

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2022-01-24
      • 2021-11-15
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多