我正在尝试将合同连接到我的私人 RPC 服务器。我希望能够更新我的 html 页面上的计数器参数,这反过来又引用了我的 solidity 合同文件和浏览器上的更新。但是,我在下面的 html 文件中不断遇到以下错误。index.html 和 contract.sol 也包括以下内容。泰!
<!doctype html>
<html>
<head>
<title>myDapp</title>
<script src="web3.js/dist/web3.min.js"></script>
<script type="text/javascript">
var contract_address = "0x68FDbd58D28BeD866E07906f6129bAC86161e243";
var contract_abi = [ { "constant": true, "inputs": [], "name": "getCreator", "outputs": [ { "name": "", "type": "address", "value": "0xc0f0fb70a63e7b345932da8eb427463f586be95d" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": false, "inputs": [ { "name": "myNewNumber", "type": "uint256" } ], "name": "setMyNumber", "outputs": [], "payable": false, "stateMutability": "nonpayable", "type": "function" }, { "constant": true, "inputs": [], "name": "getMyNumber", "outputs": [ { "name": "", "type": "uint256", "value": "3" } ], "payable": false, "stateMutability": "view", "type": "function" }, { "inputs": [], "payable": false, "stateMutability": "nonpayable", "type": "constructor" } ];
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://localhost:8545"));
}
var contract_instance = web3.eth.contract(contract_abi).at(contract_address);
console.log(contract_instance);
function getCounter() {
document.getElementById("myCounter").innerText = contract_instance.getMyNumber().toNumber().call;
}
</script>
</head>
<body>
<h1>Interact with a contract</h1>
<button onclick="getCounter()">Update Counter</button>
<button onclick="increaseCounter()">Increase Counter</button>
<span id="myCounter"></span> Counter
</body>
</html>
合同.sol
pragma solidity ^0.4.15;
contract MyContract {
address creator;
uint256 myNumber;
function MyContract() public {
creator = msg.sender;
myNumber = 3;
}
function getCreator() public constant returns(address) {
return creator;
}
function getMyNumber() public constant returns(uint256) {
return myNumber;
}
function setMyNumber(uint256 myNewNumber) public {
myNumber = myNewNumber;
}
function kill() public {
if(msg.sender == creator) {
selfdestruct(creator);
}
}
}