jquery – Ajax跨原始请求被阻止:同源策略不允许读取远程资源

前端之家收集整理的这篇文章主要介绍了jquery – Ajax跨原始请求被阻止:同源策略不允许读取远程资源前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在写一个简单的网站,作为输入一个成语,并从牛津字典中返回其含义和示例.这是我的想法:

我发送请求到以下URL:

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

例如,如果成语“不走远”,我会发送一个请求到:

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

我会被重定向到以下页面

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

在这个页面上,我可以提取成语的含义和例子.
这是我的测试代码.它将提醒响应网址:

<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.

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

解决方法

您的网站还在oxfordlearnersdictionaries.com域?或者你试图打电话给一个域,同样的原始政策阻止你?

除非您有权在oxfordlearnersdictionaries.com域上通过CORS设置标题,否则可能需要寻找另一种方法.

猜你在找的jQuery相关文章