Search code examples
javascriptwebheadercorsroblox

How to fix CORS error when getting contents from a website?


There is a CORS error in my code that gets the contents from https://roblox.com/discover to clone the roblox discover page.

My code:

<?php
   echo file_get_contents("https://www.roblox.com/discover");
?>

If I go to console, it says

Access to XMLHttpRequest at 'robloxscript' from origin 'yoursite' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

How can I go about fixing this?


Solution

  • I realized that this question is over a year old and very poorly written. I had no clue about how CORS worked in javascript. You would send the requests over to your own website and proxy them, using file_get_contents("php://input") to get the necessary postfields and getallheaders() to get the necessary headers that need to be sent. You can learn more about how CORS works here: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS