Files
ClassContent/历届学生/fontendseven/每天课程/2021-11-26/ajax.html
2024-09-27 02:06:13 +08:00

70 lines
1.7 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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>ajax</title>
</head>
<body>
<ul>
</ul>
</body>
<!-- <script>
function getData(res) {
console.log(res);
}
</script>
<script src="http://127.0.0.1/?v=getData"></script> -->
<script>
// 创建一个新的ajax对象拿出电话
var xhr = new XMLHttpRequest();
// methos 四种 请求方式
// POST 给的动作,新增
// GET 要的动作
// PUT 给的动作,更新
// DELETE 给的动作,删除
// 设置请请求方式和地址
xhr.open("POST", "http://127.0.0.1/my.php")
// 发送请求,开始拨号
// xhr.setRequestHeader("Content-Type","application/json; charset = UTF-8");
// var res = new FormData();
// res.append("id", 1)
// res.append("name", 2)
// var dara = JSON.stringify({ id: 1, name: 2 });
xhr.send()
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var data = JSON.parse(xhr.responseText)
console.log(data);
// data.result.model.res.forEach(v => {
// document.querySelector("ul").innerHTML += `
// <li>
// <p>${v.title}</p>
// <img src="${v.preview}" alt="">
// </li>
// `
// });
} else {
console.log("暂无数据");
}
}
}
</script>
</html>