以下是适用于 Safari 的 AJAX 方法,但不适用于 Chrome。错误消息如下。
$(document).ready(function(){
$('.infobutton').click(function(){
$.ajax({
url: 'http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm',
data: {
'mainSearchCriteria.v.cs': $(this).attr('data-code-system'),
'mainSearchCriteria.v.c': $(this).attr('data-code')
},
dataType: 'xml',
async: false,
method: 'GET',
success: function(data){
console.log('success');
console.log(data);
},
done: function(data){
console.log('done');
console.log(data);
},
error: function(data){
console.log('error');
console.log(data.error());
}
});
});
});
控制台输出给出 statusText
在 error
回调如下:"Error: NetworkError: DOM Exception 19"
。 This link谈论 DOM 异常 19,但这只是说 NETWORK ERROR
。没有帮助。
我从其他研究中得知 Chrome 存在安全、XSS 等已知问题。
我已经尝试了 this 中的所有建议SO帖子,以及this one 。运气不好。
使用该查询字符串直接访问 URL 是可行的。您可以查看here .
另请注意,这不是应用程序的一部分。 html 实际上就是 <button class="infobutton">Click Me</button>
。这只是使 AJAX 正常工作的概念证明。
更新
我还可以确认我在本地(使用 file:///
协议(protocol))和远程(在开发服务器上工作)都遇到了同样的错误。
最佳答案
您需要设置 crossDomain: true
,并在您的域上设置位置重定向以指向 nih.gov URL。
绕过限制的方法之一是在策略允许的域上启动请求。换句话说,它必须与发出请求的 JavaScript 位于同一域中。然后,您的页面可以将请求重定向到另一个域。
本地页面示例
<?php
// http://yourdomain.com/some/url/redirect.php
header("Location: http://apps.nlm.nih.gov/medlineplus/services/mpconnect_service.cfm");
?>
然后,将 JavaScript 中的 URL 替换为 http://yourdomain.com/some/url/redirect.php
。其效果是规避您遇到的问题。
关于jquery - 简单的 jQuery AJAX 请求在 Chrome 中不起作用(带有错误消息),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578904/