原生axjax 和axio的结合使用
固定请求地址和方法
<body>
<script>
/*
1. 新闻接口
get http://ajax-api.itheima.net/api/news
无参数
*/
function myAxios() {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
// 2.在请求行中设置请求的 地址 和方法
xhr.open("get", "http://ajax-api.itheima.net/api/settings");
// 3.注册回调函数 服务器响应内容回来之后触发
xhr.addEventListener("load", function () {
// response 响应
console.log(xhr.response);
resolve(JSON.parse(xhr.response)); //把响应回来的数据给到成功时调用的方法resolve
});
// 4.在请求体里面发送请求
xhr.send();
});
}
myAxios().then((res) => {
console.log(res);
});
</script>
axios参数的提取
<body>
<script>
/*
1. 新闻接口
get http://ajax-api.itheima.net/api/news
无参数
参数是一个对象
url 地址+get的参数(如果有的话)
method
目前只考虑get
*/
function myAxios(option) {
return new Promise((resolve, reject) => {
// method = "get" get可以默认不传
const { method = "get", url } = option;
const xhr = new XMLHttpRequest();
// 2.在请求行中设置请求的 地址 和方法
xhr.open(method, url);
// 3.注册回调函数 服务器响应内容回来之后触发
xhr.addEventListener("load", function () {
// response 响应
// console.log(xhr.response);
resolve(JSON.parse(xhr.response)); //把响应回来的数据给到成功时调用的方法resolve
});
// 4.在请求体里面发送请求
xhr.send();
});
}
myAxios({
method: "get",
url: "http://ajax-api.itheima.net/api/settings",
}).then((res) => {
console.log(res);
});
</script>
</body>
axios传递json给服务器
<body>
<h2>axios传递json给服务器</h2>
<button class="get">获取图书</button>
<button class="add">新增图书</button>
<button class="edit">修改图书</button>
<button class="delete">删除图书</button>
<script>
/*
参数是一个对象
url 地址+get的参数(如果有的话)
method
data:{} 通过请求体传递的数据 写到这里.对象的格式
*/
function myAxios(option) {
return new Promise((resolve, reject) => {
// method = "get" get可以默认不传
const { method = "get", url, data } = option;
const xhr = new XMLHttpRequest();
// 2.在请求行中设置请求的 地址 和方法
xhr.open(method, url);
// 3.注册回调函数 服务器响应内容回来之后触发
xhr.setRequestHeader("content-type", "application/json");
xhr.addEventListener("load", function () {
// response 响应
// console.log(xhr.response);
resolve(JSON.parse(xhr.response)); //把响应回来的数据给到成功时调用的方法resolve
});
// 4.在请求体里面发送请求
xhr.send(JSON.stringify(data));
});
}
myAxios({
method: "post",
url: "http://ajax-api.itheima.net/api/login",
data: {
username: "admin",
password: "123456",
},
}).then((res) => {
console.log(res);
});
//
document.querySelector(".get").onclick = () => {
myAxios({
method: "get",
url: "http://ajax-api.itheima.net/api/books",
}).then((res) => {
console.log(res);
});
};
//
document.querySelector(".add").onclick = () => {
myAxios({
method: "post",
url: "http://ajax-api.itheima.net/api/books",
data: {
bookname: "H哈哈哈",
author: "aa",
publisher: "dd",
},
}).then((res) => {
console.log(res);
});
};
//
document.querySelector(".edit").onclick = () => {
myAxios({
method: "put",
url: "http://ajax-api.itheima.net/api/books/2114",
data: {
bookname: "H哈哈哈qqqq",
author: "aa",
publisher: "dd",
},
}).then((res) => {
console.log(res);
});
};
//
document.querySelector(".delete").onclick = () => {
myAxios({
method: "delete",
url: "http://ajax-api.itheima.net/api/books/2114",
}).then((res) => {
console.log(res);
alert("已删除");
});
};
</script>
</body>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!