做网站有哪个软件好,seo整站优化一年价格多少,大连工业大学专升本,网站模板减肥目录
一、安装Axios
二、发送请求
#xff08;一#xff09;Get请求
#xff08;二#xff09;Post请求
1. 第一种方式
2. 第二种方式
三、拦截器
#xff08;一#xff09;请求前拦截器
#xff08;二#xff09;应答拦截器
四、封装 一、安装Axios -g 全局…目录
一、安装Axios
二、发送请求
一Get请求
二Post请求
1. 第一种方式
2. 第二种方式
三、拦截器
一请求前拦截器
二应答拦截器
四、封装 一、安装Axios -g 全局安装 npm i axios
在要用的模块中导入axios
import axios from axios
二、发送请求
一Get请求
// 请求头携带参数案例?uid1001
axios.get(http://localhost:8080/user/api/v1/user/query, {params: {uid: 1001}}).then(res {console.log(res.data)
}).catch(err {console.log(请求错误 err)
}).finally(() {})
二Post请求
1. 第一种方式
function funPost() {// 请求体携带参数axios.post(http://localhost:8080/user/api/v1/user/query,uid1001).then(res {console.log(res.data)}).catch(err {console.log(请求错误 err)}).finally(() {})
}
2. 第二种方式
后端需要使用RequestBody User user注解
接收json转为对象
function funPost() {// 请求体携带参数axios({url: http://localhost:8080/user/api/v1/user/query,method: post,data: {id: 1001}}).then(res {console.log(res.data)}).catch(err {console.log(请求错误 err)}).finally(() {})
}
三、拦截器
一请求前拦截器
// 请求前拦截器
axios.interceptors.request.use(function (config) {config.url config.url ?tokenABCreturn config
}, function (err) {return Promise.reject(err)
})
二应答拦截器
五、Axios全局默认值
axios.defaults.baseURL http://localhost:8000
axios.defaults.timeout 5000
axios.defaults.headers.common[Authorization] AUTH_TOKEN
axios.defaults.headers.post[Content-Type] application/x-www-form-urlencoded
四、封装 路径/api/httpRequest.js 是src目录 import axios from axios;// 设置默认值
axios.defaults.baseURL http://localhost:8000/api
axios.defaults.timeout 7000// get
function doGet(url, params) {return axios({url: url,method: get,data: params})
}// post
function doPost(url, params) {return axios({url: url,method: post,params: params})
}// 暴露方法
export {doGet, doPost}