0

I have the following code, which uses AJAX to load XML from a URL, but using that code alone results in this error: "XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access."

...Ideas, please?

5
  • 5
    The headers have to be set at the source server. If you don't control that, then your only choice is to use a proxy of your own (like what you've done). You can pass the parameter through yourself and construct the URL dynamically in the proxy code. Commented Jan 4, 2016 at 15:22
  • Yeah it's called a cross-domain request, and it's forbidden by browsers, for security reasons. The script would work if you ran it from the same domain (thegamesdb.net) but you run it from your localHost There are a few methods to bypass it (in a limited way), like JSONP... Commented Jan 4, 2016 at 15:22
  • Possible duplicate of how to bypass Access-Control-Allow-Origin? Commented Jan 4, 2016 at 15:24
  • echo file_get_contents("http://thegamesdb.net/api/GetGamesList.php?name=".$nameFromClient) Commented Jan 4, 2016 at 15:25
  • @mplungjan You are saying to put that in the phpscript.php ? How would it know what .$nameFromClient is? It would be passed from the AJAX somehow? I've never communicated between AJAX and PHP before. Commented Jan 4, 2016 at 15:40

1 Answer 1

1

Try the following

  1. Change the ajax to $.ajax({ url: "/php/phpscript.php",

  2. Change the PHP to echo file_get_contents("http://thegamesdb.net/api/GetGamesList.php?name=".$_GET["term"]);

You likely want to sanitize the $_GET["term"]

If the

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.