jQuery JSON:如何进行跨域 JSON 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5549068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JSON: How do I make cross-domain JSON call
提问by Andrew Florko
I try to run the following jquery code in local Network.
我尝试在本地网络中运行以下 jquery 代码。
$.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.
一切正常,直到“SomeSite”是本地主机。我的意思是从页面下载的同一台服务器。
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?
但是当“SomeSite”是另一个(不是本地主机)网络站点时,它看起来像是请求挂起。不会调用“错误”或“成功”回调函数。我怎样才能使这段代码工作?
Thank you in advance!
先感谢您!
回答by Claudiu
I had the same issue. Trying to get json from a server to wich I dind't had access (=> no JSONP).
我遇到过同样的问题。试图从服务器获取 json 到我没有访问权限(=> 没有 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."
我找到了http://benalman.com/projects/php-simple-proxy/将 php 代理添加到您的服务器并对这个文件进行 ajax 调用。
“任何要传递到远程 URL 资源的 GET 参数都必须在此参数中进行 urlencoded。”
$.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
proxy.php(来自 Ben Alman 的文件)托管在您的域中
替代方案(我发现它是第二好的):
http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
回答by hippietrail
Do you have server access to 'SomeSite', or is it 3rd party?
您是否可以访问“SomeSite”,还是第三方?
If you have access you can enable
CORS
wp, homeon 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, homeas 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.)
如果您有访问权限,则可以启用
CORS
wp,home就可以了。以最简单的形式(数据不是会话敏感的),只需添加标题:Access-Control-Allow-Origin: *
如果您没有访问权限,您是否知道它是否支持
JSONP
wp,那么?这通常涉及callback
在 URL中至少传递一个参数。(当然,如果您有访问权限,您也可以添加JSONP
支持。)如果您无权对“SomeSite”进行更改,并且它既不支持
CORS
也不支持JSONP
,您也许可以使用YQL
wp, home作为代理。它确实支持CORS
,JSONP
甚至可以转换数据格式,选择数据的某些部分等。
(请注意,YQL 尊重,robots.txt
因此如果它是限制自动访问的 3rd 方站点,您可能仍然不走运。)
回答by George Sibiya
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", "*".
我有一个类似的问题。我尝试了 Symba 引用的代理脚本,但由于某种原因它无法在我的机器上运行。就我而言,我试图将请求发送到托管在同一主机上的 JBoss AS 上的应用程序。不知何故,我拥有的 JBoss 版本没有办法修改响应头,以便我可以包含“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.
我使用上面的 Symba 方法解决了这个问题,但我只是在我的 Apache 服务器上设置了一个反向代理,而不是 Ben Alman 的脚本,请参阅https://www.simplified.guide/apache/configure-reverse-proxy。默认情况下,Apache 仍然会存在跨域问题。通过设置响应头“Access-Control-Allow-Origin”、“*”,请参阅http://enable-cors.org/server_apache.html,问题就消失了。
回答by bennidi
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.
我在尝试从网络服务加载 json 时遇到了同样的问题。我发现的所有 JS hack 都没有真正起作用,我想知道为什么我什至必须这样做,如果我想从我自己控制(和信任)的服务器加载数据。然后我了解到服务器的响应头在整个问题中起着至关重要的作用。当我将上面提到的标头添加到我的 web 服务的 http 响应时,问题就解决了。
回答by jimy
You can try jsonp request http://api.jquery.com/jQuery.ajax/see crossdomain
您可以尝试 jsonp 请求http://api.jquery.com/jQuery.ajax/查看 crossdomain
回答by Santosh Linkha
Please have a look at cross domain jquery ajax request. If the remote server supports JSONP then I guess you can using callback.
请查看跨域 jquery ajax 请求。如果远程服务器支持 JSONP,那么我猜你可以使用回调。
回答by Sergey Mirvoda
Actually you can only call GET.
There is no failproof way to call POST,PUT,DELETE or PATCH, via cross site scripting.
Only viable way is a hand written proxy.
实际上你只能调用GET。
没有通过跨站点脚本调用 POST、PUT、DELETE 或 PATCH 的防故障方法。
唯一可行的方法是手写代理。
回答by Mark Keats
Due to the same origin policy you can't do this. One workaround is to use the Flash AJAX jQuery plugin http://flxhr.flensed.com/that uses a Flash movie to bypass the same-origin policy.
由于同源策略,您不能这样做。一种解决方法是使用 Flash AJAX jQuery 插件http://flxhr.flensed.com/,该插件使用 Flash 电影绕过同源策略。
The other options are to proxy the requests through your own domain or use JSONP.
其他选项是通过您自己的域代理请求或使用 JSONP。