Files
ClassContent/历届学生/font-end-nine/每天上课/2022-06-26/ajax封装.html
2024-09-27 02:06:13 +08:00

72 lines
1.4 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>
</body>
<script src="./ajax.js"></script>
<script>
// var result = ajax.test().test2()
// console.log(result);
// 异步 转 同步 callback 回调
ajax.get({
url: 'http://127.0.0.1/my.php',
data: {
id: 1,
page: 2
},
header: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
}).then(res => {
console.log("2: ", res);
}).catch(err => {
console.log(err);
})
// 链式调用
// promise
// ajax 请求代码会被拆分成 3层 config 配置层serve 请求中间层ajax 封装层
var a = async () => {
console.log("1");
var res = await ajax.index({ id: 1, page: 2 });
var res = await ajax.get({
url: 'http://127.0.0.1/my.php',
data: {
id: 1,
page: 2
},
header: {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
},
});
console.log("2: ", res);
console.log("3");
}
console.log("1111");
a()
console.log("3333");
</script>
</html>