Files
ClassContent/历届学生/front-end-team-11/项目开发/上课项目/nodejs博客/fontend/test.html
2024-09-27 02:06:13 +08:00

76 lines
2.0 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 name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
<script>
// js 实现 serve 层不需要写代码也可以直接发请求
// 案例
// ajax 封装
const get = async (params) => {
params.methods = 'GET'
return params
}
// serve 中间层封装
var serve = {
// index 方法为空,没有调用 get 请求的代码哦
// app_get_hand_index
index: async (params, headers) => {
console.log("index被执行 params: ", params);
return params
},
test: async (params) => { }
}
const inject = () => {
let DescriptorList = {}
for (const key in serve) {
DescriptorList[key] = new Proxy(serve[key], {
apply: async (target, ctx, args) => {
// console.log("target: ", target);
// console.log("ctx: ", ctx);
console.log("args: ", args);
// 发送ajax请求
let res = await get(...args)
// 这行代码可以把 中间层 serve方法的 params变为ajax请求结果
// 让前端可以在 中间层 来 处理得到的响应数据,如果需要的话
// return target.call(DescriptorList[key],- res)
// return target(res)
// 这行代码 会让 中间层 serve方法 真正完全留空-
return res
}
})
}
console.log("DescriptorList: ",DescriptorList);
return DescriptorList
}
serve = inject()
// vue 页面调用虽然index方法中代码为空但请求正常发送哦
serve.index({ text: "index 方法" }, { token: 1 }).then(
res => {
console.log("res: ", res);
}
)
</script>
</html>