原生ajax示例
作者:互联网
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
window.onload = function () {
//1.获取a节点,并为其添加onclick响应函数
document.getElementsByTagName("a")[0].onclick = function () {
//3.创建一个XMLHttpRequest对象
var request = new XMLHttpRequest();
//4.准备发送请求的数据:url
var url = this.href;
var method = "GET";
//5.调用XMLHttpRequest对象的open方法
request.open(method, url);
//6.调用XMLHttpRequest对象的send方法
request.send(null);
//7.为XMLHttpRequest对象添加onreadystatechange响应函数
request.onreadystatechange = function () {
//8.判断readyState属性值为4并且status属性值为200
if (request.readyState == 4 && request.status == 200) {
//9.打印响应结果:responseText
alert(request.responseText);
}
}
//2.取消a节点的默认行为
return false;
}
}
</script>
</head>
<body>
<a href="../a.txt">hello</a>
</body>
</html>
标签:原生,function,XMLHttpRequest,示例,url,request,响应函数,ajax,var 来源: https://www.cnblogs.com/kukai/p/10853526.html