Files
ClassContent/历届学生/fontendsix/每日课程/2021-11-05/index.html
2024-09-27 02:06:13 +08:00

78 lines
1.8 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>
// 拿出手机 创建一个新的ajax对象
var xhr = new XMLHttpRequest();
// 输入号码
// 网址! 是后端给我们的,而且不止一个,每一个网址都有一个具体的功能
xhr.open("GET", "http://127.0.0.1:3000/api/index/hot")
// 拨号
xhr.send();
// 状态监听
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
// 序列化string转object/array
var data = JSON.parse(xhr.responseText)
data.result.forEach(v => {
console.log(v);
document.querySelector("ul").innerHTML += `<li>
<a href="${v.url}" target="_blank">
<img src="${v.img}" alt="">
<div class="title">${v.title}</div>
</a>
</li>`
});
} else {
alert("当前请求失败,请稍后再试")
}
}
}
//request req 请求
//response res 响应
// 请求头
// 响应头
// 请求体
// 请求方式
// GET 要的意图 读
// http://127.0.0.1:3000/api/index/class?username=刘兵&pwd=123456&age=18
// 地址栏传参
// POST 给的意图 写
// 请求体里面
// 二级制(文件流)
// form-data
// json
// PUT 给的意图,更新操作 更新
// DELETE 给的意图,删除操作 删除
// json
// xml
</script>
</html>