练习:使用ajax获取本地txt文本数据或者免费的api接口数据
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 1、创建对象
var ajax_ = new XMLHttpRequest() || new ActiveXObject('Microsoft.XMLHTT');
//2、建立连接
// ajax_.open('get','./demo.txt',true);
ajax_.open('get','https://api.apiopen.top/todayVideo',true);
//3、发送请求
ajax_.send();
//4、接收服务器数据
ajax_.onreadystatechange = function(){
/* if(ajax_.readyState==4 &&ajax_.status == 200 ){
console.log(ajax_.responseText);
} */
if(ajax_.readyState==4){
if(ajax_.status==200){
console.log(ajax_.responseText);
}else{
console.log('获取连接失败');
}
}
}
</script>
</body>
</html>
创建ajax的步骤:
-
创建Ajax对象
-
连接到服务器
-
发送请求