axios&Fetch
axios
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>axios</title> </head> <body> <!-- <script src="https://unpkg.com/axios/dist/axios.min.js"></script> --> <script src="https://unpkg.com/axios@0.19.2/dist/axios.min.js"></script> <script> // 1.axios 是什么 // axios 是一个基于 Promise 的 HTTP 库,可以用在浏览器和 node.js 中 // 第三方 Ajax 库 // http://www.axios-js.com/zh-cn/docs/ // 2.axios 的基本用法 // 引入 axios // console.log(axios); const url = 'https://www.imooc.com/api/http/search/suggest?words=js'; // axios(url, { // method: 'post', // // 请求时的头信息 // headers: { // 'Content-Type': 'application/x-www-form-urlencoded' // // 'Content-Type': 'application/json' // }, // // 通过请求头携带的数据 // params: { // username: 'alex' // }, // // 通过请求体携带的数据 // // application/json // // data: { // // age: 18, // // sex: 'male' // // } // // application/x-www-form-urlencoded // data: 'age=18&sex=male' // // timeout: 10 // // withCredentials: true // }) // .then(response => { // console.log(response); // console.log(response.data.data); // }) // .catch(err => { // console.log(err); // }); // axios // .get(url, { // params: { // username: 'alex' // } // }) // .then(response => { // console.log(response); // }); // axios // .post(url, 'username=alex&age=18') // .then(response => { // console.log(response); // }) // .catch(err => { // console.log(err); // }); axios .post('https://www.imooc.com/api/http/json/search/suggest?words=js', { username: 'alex' }) .then(response => { console.log(response); }) .catch(err => { console.log(err); }); // axios.put() // axios.delete() </script> </body> </html>
Fetch
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Fetch</title> </head> <body> <script> // 1.Fetch 是什么 // Fetch 也是前后端通信的一种方式 // Fetch 是 Ajax(XMLHttpRequest)的一种替代方案,它是基于 Promise 的 // Ajax 的兼容性比 Fetch 好 // abort timeout // 2.Fetch 的基本用法 // console.log(fetch); // console.log(ajax); // fetch() 调用后返回 Promise 对象 const url = 'https://www.imooc.com/api/http/search/suggest?words=js'; // body: (...) // bodyUsed: false // ok: true // status: 200 // statusText: "OK" // type: "cors" // url: "https://www.im // 第二个参数是对象,用来配置 fetch const fd = new FormData(); fd.append('username', 'alex'); fetch(url, { method: 'post', // body: null // body: 'username=alex&age=18', // body: JSON.stringify({ username: 'alex' }) body: fd, // headers: { // // 'Content-Type': 'application/x-www-form-urlencoded' // 'Content-Type': 'application/json' // } mode: 'cors' // credentials:'include' }) .then(response => { console.log(response); // body/bodyUsed // body 只能读一次,读过之后就不让再读了 // ok // 如果为 true,表示可以读取数据,不用再去判断 HTTP 状态码了 if (response.ok) { // console.log(response.json()); return response.json(); // return response.text(); } else { throw new Error(`HTTP CODE 异常 ${response.status}`); } }) .then(data => { console.log(data); }) .catch(err => { console.log(err); }); </script> </body> </html>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本