最近项目的开发过程中,遇到了这样的一个问题,在chrome下调试接口的时候都可以正常访问,但是在IE9下访问的时候数据都没有了,查看ajax请求过程,既然没数据,那就是请求失败了,在error的回调里打印信息,确实没进入success回调里,查阅了一些资料,得到了以下的解决方案:
1、设置浏览器安全属性,启用【通过域访问数据源】选项,步骤如下图:
设置完成之后,最好重启一下浏览器……
2、调用ajax方法时,设置crossDomain为相反的值。
$.ajax({
url: "http://dreamfactorysql.cloudapp.net/API/index.php",
dataType: "text",
async: true,
type: 'GET',
crossDomain: true == !(document.all),
success: function(txt) {
alert(txt);
}
});
3.在使用ajax请求的前一行添加以下代
jQuery.support.cors = true;
$.ajax({
})复制代码
4.调用ajax方法时,设置crossDomain为true,如下图所示:
<!DOCTYPE html>
<html>
<head>
<title>jQuery CORS in IE7 - IE10</title>
<script src="http://code.jquery.com/jquery-xxxx.min.js"></script>
<script>
$(document).ready(function() {
$.ajax({
url: "http://xxxx.php",
dataType: "text",
async: true,
type: 'GET',
cache: false,
crossDomain: true ,
success: function(txt) {
//TODO
}
});
});
</script>
</head>
<body>
IE7到IE10使用jQuery跨域!!!
</body>
</html>
$.getJSON在IE下失效的原因分析及解决方法
1.$.ajaxSetup({cache: false });
2.在请求的url后面加上时间戳或者随机数