JSON: How do I make cross-domain JSON call

68,314

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 a callback parameter in the URL. (Of course if you have access you can add JSONP support too.)

  • If you don't have access to make changes to 'SomeSite' and it supports neither CORS nor JSONP, you might be able to use YQL wp, home as a proxy. It does support both CORS and JSONP and can even translate data formats, select some part of the data, etc.
    (Note that YQL respects robots.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.

Solution 4

If you have access to the server that you want to load resources/data from you might modify the request headers of the servers response to include

"Access-Control-Allow-Origin", "*"

The Same Origin Policy enforced by the browsers - as far as I know in varying degrees of strictness depending on the browser - is (partially?) based on the values of the response headers.

I had the same issue when trying to load json from a webservice. All the JS hacks I found to get around that didn't really work and I was wondering why I even have to do this, if I want to load data from a server that I myself control (and trust). Then I learned that the response headers of the server play a vital role in this whole issue. When I added the above mentioned header to the http response of my webservice, the problem was solved.

Solution 5

Please have a look at cross domain jquery ajax request. If the remote server supports JSONP then I guess you can using callback.

Share:
68,314

Related videos on Youtube

Andrew Florko
Author by

Andrew Florko

in search of sunrise

Updated on July 09, 2022

Comments

  • Andrew Florko
    Andrew Florko over 1 year

    I try to run the following jquery code in local Network.

     $.ajax({
         type: "GET",
         url: "http://SomeSite/MyUrl/",
         cache: false,
         data: { ... },
         dataType: "json",
    
         error: function (xhr, status, error) {
                                        ... 
         },
         success: function (json) {
                                        ...
         });
    

    Everything works fine until "SomeSite" is localhost. I mean the same server from what the page was downloaded.

    But when 'SomeSite' is another (not localhost) network site it looks like request hangs. Not "error", nor "success" callback functions are called. How can I make this code work?

    Thank you in advance!

  • bennidi
    bennidi about 11 years
    You can by the way start chrome with the --disable-web-security flag to make it drop the SameOriginPolicy. This way you can also check the headers that were part of your response using chrome's network tab. This helped me a lot when debugging
  • user2718671
    user2718671 over 7 years
    The alternative worked like a charm. Thanks! But proxy php always failed, there were PHP notices about undefined optional parameters and the contents variable from the json were always null.
  • AaA
    AaA over 6 years
    What these links on hmoe and wp that each set goes to a different website supposed to do?
  • hippietrail
    hippietrail over 6 years
    "wp" = the Wikipedia page for the thing. "home" is the thing's homepage.
  • AaA
    AaA over 6 years
    your second home link is to a youtube video with lyrics I assume
  • hippietrail
    hippietrail over 6 years
    @AaA: It looks like the homepage lapsed and somebody took it and put a redirect there. I'll replace it with the biggest SO question about JSONP.

Related