Ajax跨原始请求已阻止:同源策略不允许读取远程资源 [英] Ajax Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource

查看:317
本文介绍了Ajax跨原始请求已阻止:同源策略不允许读取远程资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个简单的网站,作为输入的一个成语,并返回其意义和例子从牛津字典。这是我的想法:

I'm writing a simple site that takes as input an idiom, and return its meaning(s) and example(s) from Oxford dictionary.Here's my idea:

我向以下网址发送请求:

I send a request to the following URL:

http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=[idiom]

例如,如果成语是将发送请求到:

For example, if the idiom is "not go far", I'll send a request to:

http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=not+go+far

然后我将重定向到以下页面:

And I'll be redirected to the following page:

http://www.oxfordlearnersdictionaries.com/definition/english/far_1#far_1__192

在这个页面上,我可以提取习语的含义和例子。
这里是我的测试代码。它将提醒响应url:

On this page, I can extract the meaning(s) and the example(s) of the idiom. Here's my code for testing. It will alert the response url:

<input id="idiom" type="text" name="" value="" placeholder="Enter your idiom here">
<br>
<button id="submit" type="">Submit</button>
<script type="text/javascript">
$(document).ready(function(){
    $("#submit").bind('click',function(){
        var idiom=$("#idiom").val();
        $.ajax({
            type: "GET",
            url: 'http://www.oxfordlearnersdictionaries.com/search/english/direct/',
            data:{q:idiom},
            async:true,
            crossDomain:true,
            success: function(data, status, xhr) {
                alert(xhr.getResponseHeader('Location'));
            }
        });

    });
});
</script>

问题是我有一个错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=by+far. This can be fixed by moving the resource to the same domain or enabling CORS.

有人可以告诉我如何解决这个问题吗?
另一种方法也很好

Can anybody tell me how to resolve this please? Another approach is fine too

推荐答案

您的网站也是在oxfordlearnersdictionaries.com域上?或者您试图向域名发出呼叫,而同一来源的政策会阻止您?

Is your website also on the oxfordlearnersdictionaries.com domain? or your trying to make a call to a domain and the same origin policy is blocking you?

除非你有权限在oxfordlearnersdictionaries.com域上通过CORS设置页眉,你可能想寻找另一种方法。

Unless you have permission to set header via CORS on the oxfordlearnersdictionaries.com domain you may want to look for another approach.

这篇关于Ajax跨原始请求已阻止:同源策略不允许读取远程资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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