【发布时间】:2014-07-20 12:39:19
【问题描述】:
我正在编写一个简单的站点,该站点将一个习语作为输入,并从牛津词典返回其含义和示例。这是我的想法:
我向以下 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
在这个页面上,我可以提取成语的含义和示例。
这是我的测试代码。它会提醒响应 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>
问题是我有一个错误:
跨域请求被阻止:同源策略不允许读取位于http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=by+far 的远程资源。这可以通过将资源移动到同一域或启用 CORS 来解决。
谁能告诉我如何解决这个问题?
另一种方法也不错。
【问题讨论】:
-
你可以试试这个 - stackoverflow.com/questions/22363268/…
-
谢谢,但oxfordlearnersdictionaries.com 不是我的
-
你到底做了什么来让它工作?
标签: jquery ajax cross-domain