jsonrpc eth_getBalance 失败
Posted
技术标签:
【中文标题】jsonrpc eth_getBalance 失败【英文标题】:jsonrpc eth_getBalance fails 【发布时间】:2021-08-13 00:56:15 【问题描述】:运行以下代码时出现未定义的错误,并且 id 为空: Geth 在端口 3334 上运行,并在 rinkeby 网络上运行。任何想法为什么这会失败?运行此代码的输出是 jsonrpc:2.0;编号:空;错误:[对象对象];代码:未定义;消息:未定义;
$.ajax(
headers:
'Accept': 'application/json',
'Content-Type': 'application/json'
,
url : "http://localhost:3334",
type : 'POST',
data :
"jsonrpc":"2.0",
"method":"eth_getBalance",
"params":["0xEa0ce8c5EC357a5C6a1bF4bfC9bfA9acB4896B4e","latest"],
"id":"1"
,
dataType : "json",
success : function(result)
if (result['result'])
console.log("Balance ="+ result['result']);
else
var theText="";
for (property in result)
theText += property + ':' + result[property]+'; ';
console.log(theText);
for (property in result['error'])
theText += property + ':' + result[property]+'; ';
console.log(theText);
,
error: function (xhr, ajaxOptions, thrownError)
console.log("error");
);
【问题讨论】:
【参考方案1】:好吧,我放弃了 ajax 选项。 我找到了一种使用 XMLHttpRequest 拨打电话的方法
const xhr = new XMLHttpRequest();
// listen for `load` event
xhr.onload = () =>
// print JSON response
if (xhr.status >= 200 && xhr.status < 300)
// parse JSON
const response = JSON.parse(xhr.responseText);
console.log(response);
var temp = parseInt(response['result']);
var amount = temp / Math.pow(10,18)
console.log("Amount in ether ="+amount);
;
// create a JSON object
const json =
"jsonrpc":"2.0",
"method":"eth_getBalance",
"params":[0xEa0ce8c5EC357a5C6a1bF4bfC9bfA9acB4896B4e,"latest"],
"id":"1"
;
// open request
xhr.open('POST', 'http://localhost:3334');
// set `Content-Type` header
xhr.setRequestHeader('Content-Type', 'application/json');
// send request with JSON payload
xhr.send(JSON.stringify(json));
【讨论】:
以上是关于jsonrpc eth_getBalance 失败的主要内容,如果未能解决你的问题,请参考以下文章