JSON:如何进行跨域JSON调用 [英] JSON: How do I make cross-domain JSON call

查看:140
本文介绍了JSON:如何进行跨域JSON调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在本地网络中运行以下jquery代码。

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) {
                                    ...
     });

一切正常,直到SomeSite是localhost。我的意思是从下载页面的同一台服务器。

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

但是当SomeSite是另一个(不是localhost)网络站点时,它看起来像请求挂起。不是错误,也不是成功回调函数被调用。如何使这段代码工作?

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?

提前谢谢!

推荐答案

我有同样的问题。尝试从服务器获取json,我无法访问(=>没有JSONP)。

I had the same issue. Trying to get json from a server to wich I dind't had access (=> no JSONP).

我发现 http://benalman.com/projects/php-simple-proxy/ 将php代理添加到您的服务器,并对此文件进行ajax调用。

要传递到远程URL资源的任何GET参数必须在此参数中进行编码。

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);
   }
});

其中proxy.php(Ben Alman的文件)托管在您的域

where proxy.php (the file from Ben Alman) is hosted in your domain





(我发现这是第二好的):

http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

这篇关于JSON:如何进行跨域JSON调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆