64 lines
1.5 KiB
HTML
64 lines
1.5 KiB
HTML
<!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>js ajax学习</title>
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<ul>
|
|
|
|
</ul>
|
|
</body>
|
|
|
|
<script>
|
|
// 1 创建一个新的ajax对象 拿出手机 xhr
|
|
// fetch 全新的ajax请求方式
|
|
|
|
var ajax = new XMLHttpRequest()
|
|
|
|
// var form = new FormData();
|
|
// form.append("username", 1)
|
|
// var params = {
|
|
// id : 1
|
|
// }
|
|
var a = 'id=1'
|
|
|
|
|
|
// 2 配置需要访问的后端地址,请求方式, 设置 号码
|
|
ajax.open("POST", "http://127.0.0.1/my.php")
|
|
ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8')
|
|
// 3 发送请求 拨号
|
|
ajax.send(a)
|
|
|
|
// 监听请求状态
|
|
ajax.onreadystatechange = function () {
|
|
|
|
if (ajax.readyState == 4) {
|
|
if (ajax.status == 200) {
|
|
var data = JSON.parse(ajax.responseText)
|
|
console.log(data);
|
|
data.result.forEach(v => {
|
|
document.querySelector("ul").innerHTML += `<li>${v.title}</li>`
|
|
});
|
|
// stringify
|
|
// parse
|
|
} else {
|
|
alert("网络请求失败")
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
// function getData(res) {
|
|
// console.log(res);
|
|
// }
|
|
|
|
</script>
|
|
|
|
<!-- <script src="http://127.0.0.1/index.php?cb=getData"></script> -->
|
|
</html> |