how to make a zcash rpc call using XMLHttpRequest

127 Views Asked by At

I have being trying everything I can think of to get the request to work but always get connection refused. The curl command works, but there are no examples anywhere of anything like this where you have a username and password and another flag plus the data.

Here is what I have so far

const xhr = new XMLHttpRequest();

xhr.onload = () => {
    if (xhr.status >= 200 && xhr.status < 300) {

        const response = JSON.parse(xhr.responseText);
        console.log(response['result']);
    }
};

const json = { 
                "jsonrpc":"1.0",
                "method":"getbalance",
                "params":["*",1],
                "id":"1"
           };

xhr.open('POST', 'http://zfred:[email protected]:8232/');
xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
xhr.setRequestHeader('Content-Type', 'text/plain');
xhr.send("--data-binary",json);

I have also tried adding

xhr.setRequestHeader("Authorization", "zfred:TXlTM2NydFBAc3N3b3Jk");
xhr.withCredentials = true;

Nothing seems to work. I just get connection refused. However, this curl command works just fine.

curl --user zfred:TXlTM2NydFBAc3N3b3Jk --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getbalance", "params": ["*", 1] }' -H 'content-type: text/plain;' http://127.0.0.1:8232/

Any help would be greatly appreicated.

1

There are 1 best solutions below

0
On

Try setting the withCredentials property to true in your XMLHttpRequest.