Question

I am trying to write a jQuery script that can read a JSON file from a server that I don't have access to. My two scripts are a PHP proxy script:

<?php 

$content=file_get_contents($_GET['http://fixyourstreet.ie/api?task=incidents&by=catname&name=Litter%20and%20Illegal%20Dumping']);
echo $content;
?>

And a jQuery script:

 <script>
$.getJSON( "proxy.php", function() {
  console.log( "success" );
});
 </script>

When I run this in chrome I get the following errors in the console:

>1 OPTIONS file:///C:/xampp/htdocs/json/proxy.php Origin null is not allowed by Access-Control-Allow-Origin. jquery-1.9.1.js:8526

>2 XMLHttpRequest cannot load file:///C:/xampp/htdocs/json/proxy.php. Origin null is not allowed by Access-Control-Allow-Origin.

I don't understand why I still get a Control-Allow-Origin error even after I have made a PHP proxy script.

Any help would be much appreciated!

Was it helpful?

Solution

In your rest service before returning response set a header parameter called Access-Control-Allow-Origin to an address or public by setting *.

OTHER TIPS

file:///C:/xampp/htdocs/json/proxy.php - You obviously opened the file with jQuery from filesystem, which doesn't work with chrome, and possibly other browsers. And you need webserver to execute your proxy.php.

Solution: visit http://localhost/json/ in your brwoser.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top