一、Ajax兼容性问题
兼容版本信息:['Microsoft.XMLHTTP','MSXML.XMLHTTP','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','']
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Ajax兼容性问题</title>
</head>
<body>
<button id="btn">ie低版本Ajax</button>
<script>
// xhr对象兼容性处理
// 1. 创建xhr对象
// var xhr=new XMLHttpRequest(); //高版本
// 低版本ie
var versions=['Microsoft.XMLHTTP','MSXML.XMLHTTP','Msxml2.XMLHTTP.7.0','Msxml2.XMLHTTP.6.0','Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0','Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP',''];
for(var i=0;i<versions.length;i++){
var xhr=new ActiveXObject(versions[i]);
console.log(xhr);
// 判定是否创建成功
if(xhr){
break;
}
}
btn.onclick=function(){
// 2. 建立连接
xhr.open('get','1.php?age=22');
// 3. 发送数据
xhr.send(null);
// 4.检测数据是否发送成功,检测服务器是否响应成功
xhr.onreadystatechange=function(){
// 是否发送成功
if(xhr.readyState==4){
// 是否响应成功
if(xhr.status==200){
// 使用服务器给我们的数据
console.log(xhr.responseText);
}
}
}
}
</script>
</body>
</html>
【1.php】
<?php
echo $_GET['age'];
?>
二、Ajax工具对象:解决兼容问题与便捷问题
【 ajax.js 】
// ajax工具对象:1. 兼容问题 2. 便捷问题
function Ajax(){
// 一. xhr对象兼容问题
var xhr