我正在编写一个简单的网站,它以习语作为输入,并从牛津词典返回其含义和示例。这是我的想法:
我向以下 URL 发送请求:
http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=[idiom]
例如,如果成语是“not go far”,我将向以下地址发送请求:
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 http://www.oxfordlearnersdictionaries.com/search/english/direct/?q=by+far。这可以通过将资源移动到同一域或启用 CORS 来解决。
有人可以告诉我如何解决这个问题吗?
另一种方法也很好。