Access-Control-Allow-Origin不允许原始'url' [英] Origin 'url' is not allowed by Access-Control-Allow-Origin

查看:163
本文介绍了Access-Control-Allow-Origin不允许原始'url'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Google货币计算器使用以下jquery(dummy)代码获取货币汇率:

  $ .getJSON(http://www.google.com/ig/calculator?hl=zh-TW&q=1+DOP+=?+ USD,
功能(数据){
$('。currNumber')。每个(函数(指数){
$(本)的.html(parseFloat($(本)。 html())* 0.02681);
});
});




XMLHttpRequest的不能加载的http://www.google.com/ig/calculator?hl=zh-TW&q=1DOP=?USD


查看网站我发现了多个主题主题,但他们大多是参考本地文件访问,并尝试通过启动chrome与一个额外的参数(我也使用chrome)解决它,但这不是我的问题,这实际上似乎更多与跨域限制相关。 p>

所以,问题是:我如何使用jQuery从那个网址


解决方案<率是多少? / div>

Ajax请求受浏览器的同源策略限制。简而言之,这意味着您不能通过与运行脚本的网页位于同一域的ajax直接与服务器通信。因此,除非您正在开发google.com的网页,否则不能直接与google.com通话。



这个限制涉及插入脚本标记的解决方法(通过脚本标记加载的JS文件不受相同的源策略),然后使用JSONP回调从这些脚本标记将数据结果传回到主脚本。这可能是你在这里需要做的,如果你尝试使用的API支持它。



jQuery将帮助你在这里,因为它可以自动打开ajax调用通过脚本标记加载的JSONP调用,并且可以在跨域情况下工作。根据 jQuery文档的ajax函数,如果它在参数中看到callback =,它将自动执行此操作ajax调用的字符串,或者如果您设置了crossDomain选项。


I'm trying to get a currency rate with the Google Currency Calculator using the following jquery (dummy) code:

    $.getJSON("http://www.google.com/ig/calculator?hl=en&q=1" + "DOP" + "=?" + "USD",
        function(data) {
            $('.currNumber').each(function (index) {
                $(this).html(parseFloat($(this).html()) * 0.02681);
                                  });
          });

XMLHttpRequest cannot load http://www.google.com/ig/calculator?hl=en&q=1DOP=?USD. Origin 'hostURL' is not allowed by Access-Control-Allow-Origin.

Looking in the site I've found various topics on the subject but they mostly refer to local file access and attempt to solve it by starting chrome with an additional parameter (I'm also using chrome) but such is not my issue, which actually seems more related to cross-domain restrictions.

So, question is: How can I use jQuery to get the rate from that url?

解决方案

Ajax requests are limited by the browser's Same Origin Policy. In a nutshell, that means that you can't talk directly to a server via ajax that isn't on the same domain as the page your script is running in. So, unless you're developing a page for google.com, you can't talk to google.com directly.

There are work-arounds for this limitation involving the insertion of script tags (JS files loaded via script tags are not subject to the same origin policy) and then using JSONP callbacks to communicate data results back to your main script from those script tags. That is probably what you need to do here if the API you're attempting to use supports it.

jQuery will help you a lot here as it can automatically turn an ajax call into a JSONP call that is loaded via script tags and can work in cross domain situations. According to the jQuery doc for it's ajax function, it will do this automatically if it sees "callback=" in the parameter string for the ajax call or if you set the crossDomain option.

这篇关于Access-Control-Allow-Origin不允许原始'url'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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