[Ethereum] web3.isConnected() returns false when connecting to geth with web3.js

go-ethereumjson-rpcwalletsweb3js

I am totally new to cryptocurrency stuff. I have installed geth and web3. I want to build a web page showing account balance. I start geth with below command

build/bin/geth --rpc  --rpcaddr 127.0.0.1 --rpcport 8545 --rpcapi web3,db,eth,net,personal

[ec2-user@ip ethereum]$ curl http://127.0.0.1:8545  
[ec2-user@ip ethereum]$ curl http://127.0.0.1:8545  
[ec2-user@ip ethereum]$ sudo netstat -nlp | grep :8545   tcp        0      0 127.0.0.1:8545              0.0.0.0:*                   LISTEN      18238/build/bin/get

My code in stest.html is:

<script src="web3.js/dist/web3.js"></script> 
<script>  
if (typeof web3 !== 'undefined') {  
  web3 = new Web3(web3.currentProvider);  
} else {  
  // set the provider you want from Web3.providers  
  var web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));  
  //var web3 = new Web3('http://localhost:8545'); // TypeError:   this.provider.send is not a function
}  
console.log(web3); // {eth: .., shh: ...} // it's here!  
if(!web3.isConnected()) {  
  console.log("fail");  
} else {  
  console.log("succ");  
}  
//var coinbase = web3.eth.coinbase;  
//var balance = web3.eth.getBalance(coinbase);  
//console.log(web3.eth.accounts);  
var version = web3.version.api;  
console.log(version); // "0.2.0"  
var version = web3.version.node;  
console.log(version);

I get the below errors in the browser console when I run stest.html:

Object { _requestManager: {…}, currentProvider: {…}, eth: {…}, db: {…}, shh: {…}, net: {…}, personal: {…}, bzz: {…}, settings: {…}, version: {…}, … }
stest.html:13:1


fail

0.20.5

Error: CONNECTION ERROR: Couldn't connect to node http://127.0.0.1:8545.
web3.js:3118:16

Source map error: request failed with status 404
Resource URL: http://34.215.167.9/plugins/ethereum/web3.js/dist/web3.js
Source Map URL: web3.js.map

Below is the info I got from the terminal:

[ec2-user@ip go-ethereum]$ build/bin/geth version  
Geth
Version: 1.8.0-unstable   
Git Commit: 722bac84fa503199b9c485c1a3e2bfba03bc487d  
Architecture: amd64  
Protocol Versions: [63 62]  
Network Id: 1  
Go Version: go1.8.4  
Operating System: linux  
GOPATH=
GOROOT=/usr/lib/golang
  1. Sorry for the question format. I wanted to add as info as possible. Please edit question or ask for more info.
  2. How can I avoid the "Connection Error?"
  3. Is this method of connecting to Ethereum correct?
  4. How can I improve my code?
  5. I have tried a few questions here, but no answers resolve my error.

UPDATE:
Now CURL gives result after starting with below geth command

build/bin/geth --rpc --rpccorsdomain "http://localhost:8545"

$ curl -H "Content-Type: application/json" -X POST --data '{"jsonrpc":"2.0","method":"web3_clientVersion","params":[],"id":67}' http://127.0.0.1:8545  
{"jsonrpc":"2.0","id":67,"result":"Geth/v1.8.0-unstable-722bac84/linux-amd64/go1.8.4"}

But in browser console error changed to

ReferenceError: Web3 is not defined

Best Answer

One possibility is CORS issue. Can't be sure with given information. Add the following option to your geth startup command

--rpccorsdomain "*"