JSON: How do I make cross-domain JSON call

前端 未结 8 890
情歌与酒
情歌与酒 2020-11-30 06:54

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

 $.ajax({
     type: \"GET\",
     url: \"http://SomeSite/MyUrl/\",
     cache: false,
     data: {         


        
8条回答
  •  眼角桃花
    2020-11-30 07:28

    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/

提交回复
热议问题