CORS Problem when set image.source = url

I set the image.source to some asset url held by a server, it returns CORS problem. I know what is CORS, my question is that without change the server’s CORS settings, can I set a JWT along with Babylonjs to solve the CORS problem? Or can I download the image firstly then assign it to image?

You can do anything the server allows you to do. If the server requires some header manipulation in order for it to work, you can add the header to every web request using this Map: WebRequest | Babylon.js Documentation . It is a static map and will be applied to every request made!
Another way (more robust, but of course a little more complicated to implement) is using the request modifiers: WebRequest | Babylon.js Documentation
This is an array of callbacks, that get the request object and the url that is being requested, and allow you to manipulate the XMLHttpRequest object according to your needs, based on the URL. so you could add a function doing (in pseudo code!)

if(this-is-an-image(url))
  add-jwt-to-request(request)

and you are good to go.

3 Likes