自己在做原生ajax请求时发生了意外,在ie8浏览器运行时提示拒绝访问。
下面是测试代码
<script>
function ajax(id){
var url="http://localhost/www/jsonhandle.php?id="+id;
var xmlObj =null;
if(window.XMLHttpRequest){
xmlObj =new XMLHttpRequest();
}else{
xmlObj =new ActiveXObject("Microsoft.XMLHTTP");
}
xmlObj.open("GET",url,true);
xmlObj.send(null);
xmlObj.onreadystatechange=function(){
if(xmlObj.readyState==4 && xmlObj.status==200){
var obj = JSON.parse(xmlObj.responseText);
alert(obj);
}
}
}
</script>
<button onclick="javascript:ajax(3);">AJAX请求示例</button>
百度+google了N多网站,终于在我们可爱的优快云论坛上找到答案,原来是
ie8 不支持 js中带的http://
只能使用相对路径,比如./。
所以只要将URL链接改为"./www/jsonhandle.php?id="就可以了
但是这样做就不能跨域访问了。
所以项目中一般还是用jQuery比较好
为了怕以后忘记,特写在博客上,