简体   繁体   中英

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.

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. Replace http://my_ip_address:8545 with 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. Can you show the output of running ganache-cli in the commandline?

And, where you have 'my_ip_address' You can simple replace that with http://localhost:8545/

Can you also, in a browse, go to http://localhost:8545 If it works, you should see a '400 Bad Request'. If it does not work, you will see a site cannot be reached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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