CORES problem?

#1

I have a website that runs javascript in a browser on my free amazon EC2 server.

The Javascript in the browser makes php calls to the amazon ec2 server, causing the php script to fetch data from another server, passing it back to the javascript in the browser.

for example the javascript in the browser would call the php code on the server like this

http://ec2.24-44-55-amazonaws.com/hello.php?fetch=http://www.server1.com/file.mp4

This would make the php code fetch file.mp4 from server1.com and pass it to the javascript running browser.

All that works fine.

Now i decide to assign a nice domain to the amazon ec2 and run that domain through cloudflare for security.

So i buy a new domain called myDomain from go daddy and assign that to the amazon ec2 server.

Then run mydomain.com through a free cloudflare account for security reasons.

So the javascript in the browser would now call

http://www.mydomain.com/hello.php?fetch=http://www.server1.com/file.mp4

This should do the same thing as before, except go through cloudflare to amazon ec2, and then to the php script which would fetch the file.mp4 from server1.com and pass it back through cloud flare to the browser.

But now for some reason the browser crashes when the javascipt make the call to the php giving an uncaught security error saying that cross-origin data may not be loaded

Actually it crashes when im fetching a streamed video which works when i dont use cloudflare.

Anybody knows what is the problem and how to fix??

Thanks

#2

You may forget to change URL for some requests from ec2.24-44-55-amazonaws.com to mydomain.com.

If JavaScript comes from mydomain.com it should work. Otherwise you need to set Access-Control-Allow-Origin header for mydomain.com.

#3

Hello Xaq,

Thanks for the suggestion, but all the requests are generated from a single place in the javascript, so there is no chance of some requests going to the old URL.

Yes Javascript comes from mydomain.com, so im confused why it dont work.

Any other suggestions?

#4

No more Idea unless we know what is the domain and check it. You can do that yourself, check network tab of browser dev tools.

#5

Hello Xaq,

How do i set Access-Control-Allow-Origin header for my domain?

Thanks

#6

You need to set that header where the php code is hosted (via php or .htaccess).

#7

Do you mean something like

<?php ..... header('host : www.mydomain.com'); ..... ?>
#8
<?php
header('Access-Control-Allow-Origin: https://mydomain.com');
?>
1 Like
#9

Hello Xaq,

Thanks for the help, i added that to my php script, it seems to work for a while (it used to crash right away). Now it works for a while, maybe i play a few videos, then it randomly crashes with the same error. Here is the error…

exception thrown: SecurityError: Failed to execute ‘texImage2D’ on ‘WebGL2RenderingContext’: The video element contains cross-origin data, and may not be loaded.,Error: Failed to execute ‘texImage2D’ on ‘WebGL2RenderingContext’: The video element contains cross-origin data, and may not be loaded.
at _WebGLMovieTextureUpdate

Thanks for any suggestions, im so desperate!

#10

Seems you need to add more origins. Without knowing the domain you are on your own to find that. You can test following code to make sure problem is what we are talking about:

<?php
header('Access-Control-Allow-Origin: *');
?>

Remember above code should be replaced with origins you know, otherwise this can be used to run malicious code by hackers. Also check this answer on SoF, You may fix the issue without setting header to * with that.

closed #11

This topic was automatically closed after 31 days. New replies are no longer allowed.