0

I get an undefined error and the id is null when running the following code: Geth is running on port 3334 and is running on the rinkeby network. Any ideas why this is failing? the output of running this code is jsonrpc:2.0; id:null; error:[object Object]; code:undefined; message:undefined;

$.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");
          } 
        });
Jeff
  • 137
  • 1
  • 6

1 Answers1

0

Well I gave up on the ajax option. I found a way to make the call using 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));
Jeff
  • 137
  • 1
  • 6