JSON: How do I make cross-domain JSON call
Solution 1:
I had the same issue. Trying to get json from a server to wich I dind't had access (=> no JSONP).
I found http://benalman.com/projects/php-simple-proxy/ Add the php proxy to your server and do the ajax call to this file.
"Any GET parameters to be passed through to the remote URL resource must be urlencoded in this parameter."
$.ajax({
type: 'GET',
url:'proxy.php?url=http://anyDomain.com?someid=thispage',
dataType: "json",
success: function(data){
// success_fn(data);
},
error: function(jqXHR, textStatus, errorThrown) {
// error_fn(jqXHR, textStatus, errorThrown);
}
});
where proxy.php (the file from Ben Alman) is hosted in your domain
Alternative (which I found to be second best to this):
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
Solution 2:
Do you have server access to 'SomeSite', or is it 3rd party?
If you have access you can enable
CORS
wp, home on it. In its simplest form (data is not session sensitive), just add the header:Access-Control-Allow-Origin: *
If you don't have access do you know whether it supports
JSONP
wp, so? This typically involves passing at least acallback
parameter in the URL. (Of course if you have access you can addJSONP
support too.)If you don't have access to make changes to 'SomeSite' and it supports neither
CORS
norJSONP
, you might be able to useYQL
wp, home as a proxy. It does support bothCORS
andJSONP
and can even translate data formats, select some part of the data, etc.
(Note that YQL respectsrobots.txt
so if it's a 3rd party site that restricts automated access you might still be out of luck.)
Solution 3:
I had a similar issue. I tried the proxy script quoted by Symba but for some reason it could not work on my machine. In my case I was trying to send request to an app hosted on a JBoss AS on the same host. Somehow the version of JBoss I had did not have a way to modify response headers so that I could include "Access-Control-Allow-Origin", "*".
I solved it by using Symba's approach above but instead of Ben Alman's script I just set up a reverse proxy on my Apache Server, see https://www.simplified.guide/apache/configure-reverse-proxy . By defaults Apache would still have cross domain issues. By setting the response header "Access-Control-Allow-Origin", "*", see http://enable-cors.org/server_apache.html, the problem goes away.