how to use proxy with JSON

2019-08-22 21:12发布

问题:

I have a php page called 'dataFetch.php' which sits on one webserver. On another webserver, I have a JS file which issues JSON calls to dataFetch. dataFetch connects to a database, retrieves data and puts it in a JSON format which is fed back to the calling program. In IE, this works fine. In other browsers it does not because of the cross domain restriction.

To get across the cross-domain restriction, I make a call to a file, proxy.php, which then makes the call to dataFetch. My problem now is that proxy.php retrieves the file from dataFetch but the JS script file no longer sees the response from proxy.php as a JSON format and so I can't process it. Can anybody help me out?

回答1:

Have a look at using JSONP instead, which solves the cross site difficulties you have had.

Please explain how the proxy works. A proxy should be very simple, something like this:

<?php
$url = $_GET['ur'];
echo file_get_contents($url);
?>

And used like this:

http://www.example.com/proxy.php?url=http://www.someothersite.com/dataFetch.php