简体   繁体   English

使用Web 3连接到智能合约时出错

[英]Getting error while connecting to smart contract using web 3

i referred https://medium.com/@amkurian/interacting-with-ethereum-smart-contracts-through-web3-js-e0efad17977 to connect to smart contract, where i want to simply access the get() method of the smart contract. 我引用了https://medium.com/@amkurian/interacting-with-ethereum-smart-contracts-through-web3-js-e0efad17977连接到智能合约,在这里我想简单地访问智能合约的get()方法合同。

Here is the code: 这是代码:

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

Kindly help 请帮助

EDITED Answer: 编辑答案:

Run the command ganache-cli and leave it running. 运行命令ganache-cli并使其运行。 Replace http://my_ip_address:8545 with http://localhost:8545/ http:// my_ip_address:8545替换为http:// localhost:8545 /

This will resolve your problem. 这样可以解决您的问题。

Previous Deleted Answer: 先前删除的答案:

The problem is, presumably, your ganache local node not being configured properly, or not running. 问题可能是您的ganache本地节点配置不正确或未运行。 Can you show the output of running ganache-cli in the commandline? 您可以在命令行中显示运行ganache-cli的输出吗?

And, where you have 'my_ip_address' You can simple replace that with http://localhost:8545/ 并且,如果您拥有“ my_ip_address”,则可以简单地将其替换为http:// localhost:8545 /

Can you also, in a browse, go to http://localhost:8545 If it works, you should see a '400 Bad Request'. 您还可以在浏览器中转到http:// localhost:8545。如果可行 ,您应该会看到“ 400错误的请求”。 If it does not work, you will see a site cannot be reached. 如果它不起作用,您将看到无法访问该站点。

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

相关问题 在 Hedera 区块链中创建智能合约时出现错误“交易过大” - Getting error "Transaction Oversize" while creating a smart contract in Hedera blockchain 使用Node.js连接到mongoDB数据库时出错 - Getting error while connecting to mongoDB database using Node.js 使用 web3 调用智能合约函数的问题 - Issue in calling a smart contract function with web3 调用智能合约方法时如何摆脱 web3 上的 504 超时错误? - How can I get rid of 504 timeout error on web3 when calling a smart contract method? 使用 Node.js 编译和部署以太坊智能合约时出错 - Getting error when compiling and deploying ethereum smart contract with Node.js 使用 Ganache-Cli、Mocha、Web3、Solc 0.8.6 编译器运行智能合约 - Running smart contract using Ganache-Cli, Mocha, Web3, Solc 0.8.6 compiler 如何使用 nodejs 上的 Web3js 解码在以太坊上调用智能合约的响应 - How to decode response from calling smart contract on Ethereum using Web3js on nodejs 使用 nodejs 进行智能合约测试失败并显示错误消息 - Smart Contract test with nodejs fails with an error message Mongodb 和节点在连接集群时出错? - Mongodb and node Getting error while connecting with cluster? 使用Node.js连接数据库时出错 - Getting error while connecting database with nodejs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM