原生JS实现 Ajax
1. 运行服务器
2. 案例需求
3. 相关语法
1. 创建ajax对象
let xhr = new XMLHttpRequest();
2. 告诉ajax请求方式和请求地址
xhr.open(请求方式,请求地址);
3. 发送请求
xhr.send();
4. 获取服务器返回的数据
xhr.onload=function(){
xhr.responseText;
}
4. 代码实现
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>原生js实现ajax</title>
<style>
span{
color: red;
}
</style>
</head>
<body>
<input id="username" type="text" placeholder="请输入用户名"><span id="userwarn"></span>
<script>
document.getElementById('username').onblur=function () {
console.log(this.value);
let xhr = new XMLHttpRequest();
xhr.open('get','http://localhost:8080/check?username='+this.value)
xhr.send();
xhr.onload=function () {
console.log(xhr.responseText);
document.getElementById('userwarn').innerText=xhr.responseText;
}
}
</script>
</body>
</html>
原文链接:https://qwert.blog.csdn.net/article/details/105456834