<!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>