JSON:如何进行跨域JSON调用

编程入门 行业动态 更新时间:2024-10-11 15:22:48
本文介绍了JSON:如何进行跨域JSON调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

$.ajax({ type: "GET", url: "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).

我发现 benalman/projects/php-simple-proxy/ 将php代理添加到您的服务器,并对此文件进行ajax调用。 要传递到远程URL资源的任何GET参数必须在此参数中进行编码。

I found benalman/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=anyDomain?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

(我发现这是第二好的): james.padolsey/javascript/cross-domain-requests-with-jquery/

更多推荐

JSON:如何进行跨域JSON调用

本文发布于:2023-11-28 12:43:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642445.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!