0

I'm getting this error:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8545/. (Reason: CORS request did not succeed).

I'm aware of this GitHub issue: https://github.com/ethereum/web3.js/issues/1802 But my web3 version is 1.2.6. So I think this bug was already fixed, because this GitHub issue is 2 years old.

I'm trying to connect to MetaMask like this:

 var web3;
        if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
            console.log("web3 was not undefined");
        } else {
            web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
            console.log("We are not in the first 'if' statment.");
        }

        //long part with ABI

        EthereumSession.methods.BuyTicket().send({ from: web3.eth.defaultAccount })
        .then(receipt => { console.log("We are in the 'then'"); });

This will give error No "from" address specified, if I set an address with web3.eth.defaultAccount = '0x...'; I will get error about Cross-Origin Request

~~This is a simple HTML file with < script> tags, web3 is installed with npm install web3.~~

UPDATE:

(not running as file anymore) Now I'm hosting it in localhost. I removed node_modules folder with web3, now I see the MetaMask injected Web3. This changed some errors, now I need to use different version of EthereumSession.at

Now I'm getting a TypeError: this._eth.sendTransaction is not a function error for this line:

ethereumSessionInstance.BuyTicket({
            from: web3.eth.defaultAccount,
            gas: 100000,
            value: web3.toWei(0, 'ether')
        });

BuyTicket() is a Solidity function that has no arguments.

I'm also getting 'Source Map Error' warnings.

  • 1
    What's the domain used to access the page? Is it localhost? FYI. ethereum.enable() might be required to access metamask. – AlexW Mar 26 at 1:12
  • no, I'm opening a file like "file:///home/user/App-1/TicketPurchaser/index.html" – Imestin Mar 26 at 8:37
  • I wanted to try the easiest way, create a smart contract, deploy to Ropsten, and access it with minimal code. This is not running as a NodeJS app. Is it necessary to create NodeJS app for accessing web3? – Imestin Mar 26 at 8:46
  • 1
    It doesn't have to be a NodeJS app but the index.html needs to be hosted by a web server and accessed with http or https. This way it will have an access origin filled. This post explains it more: stackoverflow.com/questions/40204913/… – AlexW Mar 27 at 0:12
  • 1
    Give a go mate and let me know if it doesn't work. Cheers. – AlexW Mar 27 at 0:13
1

I'd like to return to the first issue:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:8545/. (Reason: CORS request did not succeed).

If you're trying to connect to localhost:8545, I assume that you're trying to connect MetaMask to a local node.

Essentially, MetaMask is trying to connect to localhost:8545, but can't. Geth, and I believe all the other clients, lock down cross-origin requests to the RPC by default. In fact, RPC may not be open by default, either. On Geth, the flag is --rpc.

You'll also need to enable MetaMask to make a cross-origin request. I feel somewhat guilty plugging myself, but I wrote an answer here about how to do this. It differs from browser to browser (as in, the precise name of the domain that you're going to allow to access the RPC data changes depending on where and to what MetaMask is installed).

I hope this is enough to fix the problem!

| improve this answer | |
  • No, I'm trying to interact with a smart contract that is deployed to Ropsten. geth is not even installed. The smart contract was deployed with Remix, and I'm trying to create a really basic interface, which will interact with this smart contract. Next time I will most likely create a Truffle project, but I want to know what are the minimum requirements to interact with a smart contract. – Imestin Mar 27 at 11:21

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.