<!DOCTYPE HTML><HTML lang="en"><head> <Meta charset="UTF-8"> <Meta name="vIEwport" content="wIDth=device-wIDth,initial-scale=1.0"> <Meta http-equiv="X-UA-Compatible" content="IE=edge"> <Title>document</Title></head><body> <script src="./axios.min.Js"></script> <script> // // axios对象 axios.get(url) .then((res) => { /*请求成功 会来到这 res响应体*/ }) .catch((err) => { /*请求失败 会来到这 处理err错想*/ }) axios.get(url) .then((res) => {/*请求成功来这里执行代码*/ }) .catch((err) => { /*请求失败会来这里执行代码*/ }) // GET查询数据 axios .get('http://127.0.0.1:3000/brands') .then((res) => { console.log(res); }) // 查询ID为4的一条数据 axios.get('http://127.0.0.1:3000/brands/4') .then((res) => { console.log(res.data); }) // 查询ID为1的一条数据 axios .get('http://127.0.0.1:3000/brands/1') .then((res) => { console.log(res.data); }) // // POST 添加数据 axios .post("http://localhost:3000/brands",{ name: parseInt(Math.random() * 20),date: (new Date()).tolocaleString() }) .then((res) => { console.log(res.data); }) // // // POST 添加数据 axios .post('http://127.0.0.1:3000/brands',{ name: 'abccccc',date: new Date() }) .then((res) => { console.log(res); }) // PUT 修改数据 和post用法一样 axios .put("http://localhost:3000/brands/20",{ name: "eeeee",date: new Date() }) .then((res) => { console.log(res); }) // DELETE 删除 axios .delete("http://localhost:3000/brands/20") .then((res) => { console.log(res); }) // // GET 模糊搜索 axios .get("http://localhost:3000/brands?name_like=" + "aa") .then((res) => { // console.log(res); // 判断状态 // const let // 建议全用const 如果报错 -> 改成let const { status,data } = res; if (status === 200) { console.log(data); } }) .catch((err) => { }) // 模糊搜索 var cha = 'ab' axios .get("http://localhost:3000/brands?name_like=" + cha) .then(res => { const { status,data } = res if (status === 200) { console.log(data); } }) </script></body></HTML>总结
以上是内存溢出为你收集整理的11-基础-axios-使用全部内容,希望文章能够帮你解决11-基础-axios-使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)