2

I'm following along with this short tutorial and ran into a few problems. I'm running testrpc in my console. However, every time I run my index.html I get an error. My index.html file is the following:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>



    <script src="./node_modules/web3/dist/web3.min.js"></script>

</head>
    <body>
        <div class="container">

            <h1>Coursetro Instructor</h1>
        </div>

        <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

        <script>

            if (typeof web3 !== 'undefined') {
                web3 = new Web3(web3.currentProvider);
            } else {
                 // set the provider you want from Web3.providers
                 web3 = new Web3(newWeb3.providers.HttpProvider("http://localhost:8545"));
             }
             web3.eth.defaultAccount = web3.eth.accounts[0];
        </script>

    </body>
</html>

I get the 2 errors in the console: Failed to load http://localhost:8545/: Response to preflight request doesn't pass access control check: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'. Origin 'null' is therefore not allowed access. The credentials mode of requests initiated by the XMLHttpRequest is controlled by the withCredentials attribute.

and Uncaught Error: CONNECTION ERROR: Couldn't connect to node http://localhost:8545.

both occur only when I try to execute web3.eth.accounts[0].

The weirdest part about all of this is the fact that this was running fine last night. However, after closing chrome, I opened this up again and I had this error. Anybody know what to do?

Thank you so much in advance

2
geth --rpc --rpcaddr "localhost" --rpcapi "web3,eth" --rpccorsdomain "*" --rpcvhosts "*"

Go(https://github.com/ethereum/wiki/wiki/JSON-RPC#go)

You can start the HTTP JSON-RPC with the --rpc flag

geth --rpc

If accessing the RPC from a browser, CORS will need to be enabled with the appropriate domain set. Otherwise, JavaScript calls are limit by the same-origin policy and requests will fail:

geth --rpc --rpccorsdomain "http://localhost:3000"

  • Hi, I'm struggling with this issue. Here are my steps: 1. use Ganache console to start a local blockchain. "127.0.0.1:7545" 2. truffle compile --network my_ganache 3. truffle migrate --network my_ganache 4. truffle console --network my_ganache I can successfully call like 'web3.eth.account[0]' by far. But then I use "npm run dev" to start the server and I get the same problem: "Cannot use wildcard in Access-Control-Allow-Origin when credentials flag is true.", "XMLHttpRequest cannot load localhost:7545 due to access control checks.".. How can I do to fix this? thanks! – Jerry Gu Sep 27 '18 at 9:18
2

This is easily solved by installing this version of web3:

npm install web3@0.20.6 --save

Courtesy to Bereket-gebredingle

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.