无法通过前端 Javascript 中的 web3.js 调用智能合约功能

问题描述

我使用前端 Javascript 而不是 Node 来与 Ganache 上部署的合约进行交互。 NodeJS 版本完美运行,但前端 JS 版本不行。无法调用所有功能代码如下:

<script src="/node_modules/web3/dist/web3.min.js"></script>

<script>
        walletAddress = null;
        //Connecting to Blockchain
        var web3 = new Web3(Web3.givenProvider || "HTTP://127.0.0.1:7545");
        var contactAddress = '0xB16bEc01bfe4F13D5e85A2F75F51893D797Df1F7'
        var abi = '[{"constant":true,"inputs":[],"name":"getValue","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"internalType":"uint256","name":"_value","name":"setValue","outputs":[],"stateMutability":"nonpayable",{"constant":true,"name":"getMsgSender","outputs":[{"internalType":"address","type":"address"}],"type":"function"}]'
        const abiJson = JSON.parse(abi);

        let init = async () => {
            ac = await web3.eth.getAccounts()
            walletAddress = ac[0]
            const contract = new web3.eth.Contract(
                abiJson,contactAddress
            );
            console.log(await contract.methods.setValue(900).send({from:walletAddress}));
            
            contract.methods.getValue().call().then(function (result) {
                console.log(result);
            });

        }
        init()
    </script>

没有调用任何函数。不知道怎么判断是不是真的在和Ganache互动。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...