2

I'm getting this error in Console OPTIONS http://localhost:8545/ net::ERR_CONNECTION_REFUSED ,Error: Error: Invalid JSON RPC response: "" when trying to execute this piece of code from a webpage on VPS (http://example.com/test-app/web3-test.html):

   <html>
    ......
    <body>
     <button class="btn btn-primary" onclick="createAccount();">Create Account</button>
    .....

    <script src="/js/web3.js-1.0.0-beta.34/dist/web3.min.js"></script>
    <script type="text/javascript">

    //var Web3 = require('web3');
    var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'))

    function createAccount(){
        web3.eth.personal.newAccount('123456789abc', function(err, res){
            console.log("Error: "+err);
            console.log("Res: "+res);
        });
    }

    </script>
</body>
<html>

I have installed already ethereum on my VPS.

sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum

And geth --rpc is displaying this in terminal(connected with live server through ssh)

INFO [04-17|20:14:09] IPC endpoint opened                      url=/root/.ethereum/geth.ipc
INFO [04-17|20:14:09] HTTP endpoint opened                     url=http://127.0.0.1:8545    cors= vhosts=localhost

I guess its a CORS issue, is there geth default setting to fix?(I don't want to install CORS extension for temp fix also I'm not using Node js, its a simple web page where I included web3 version 1 file)

Also tried this: geth --rpc --rpccorsdomain "*" --rpcapi db,eth,net,web3,personal

And output in terminal is:

INFO [04-17|20:52:32] IPC endpoint opened                      url=/root/.ethereum/geth.ipc
INFO [04-17|20:52:32] HTTP endpoint opened                     url=http://127.0.0.1:8545    cors=* vhosts=localhost

Thank you everyone...

  • that gonna work if you open your browser inside the VPS. If you navigate to your VPS page through your PC localhost is you not the VPS. So you might need to change localhost with the VPS public address – mirg Apr 18 '18 at 3:03
  • Thank you how could I change localhost with the VPS public address? – Muhammad Shahzad Apr 18 '18 at 5:48
  • just change this line: var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')) but the VPS should also listen for incoming connections. – mirg Apr 18 '18 at 5:57
  • @mirg how I can prevent other user to not use my 64.421.252.5:8545 address in their pages? only my page can access this port on my server? – Muhammad Shahzad Apr 18 '18 at 6:35
  • 1
    then you have to create a backend that interact with your node instead of using it in the client side. – mirg Apr 18 '18 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.