CORS issue on Mumbai RPC

Hi there,

I’m trying to implement a simple web wallet plugged into Polygon blockchain. I instanciate web3:

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers
  .HttpProvider("https://rpc-mumbai.matic.today/"))

But when I do XHR requests to get my balance, my web browser blocks the request because the RPC server does not have the right header in its response:

Access to XMLHttpRequest at ‘https://rpc-mumbai.matic.today/’ from origin ‘http://forum.polygon.technology’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

This looks to me like a misconfiguration on rpc-mumbai.matic.today side, as this endpoint surely should be open to any site.

Could this be fixed? Is there a work around?

Thanks in advance for any reply. Keep up the good work!!!

Thank you for your king reply and pointing out that the httpProvider was deprecated. Using the web socket url works great inside the browser :

const Web3 = require('web3');
const web3 = new Web3('wss://ws-mumbai.matic.today');
let balance = await web3.eth.getBalance("0x1AeB179ADf7a87EdC7e3f63859743AE088ff5683");
const ethBalance = web3.utils.fromWei(balance, "ether");
console.log(ethBalance);

outputs “0.1” from my Nuxt application (so it’s the actuel web browser that connects to mumbai web service, and not any nodeJS application, which is exactly what I wanted).

Thanks again !! :smile: :sunny: