127.0.0.1/:1Access to XMLHttpRequest at 'http://127.0.0.1:8000/sers/gensetview/'from origin 'http://127.0.0.1:8080' has been blocked by CORSpolicy: Request header field x-custom-header is not allowed by Access-Control-Allow-Headersin preflight response.
// Add a request interceptor axios.interceptors.request.use(function (config) { // Do something before request is sent return config; }, function (error) { // Do something with request error returnPromise.reject(error); });
// Add a response interceptor axios.interceptors.response.use(function (response) { // Any status code that lie within the range of 2xx cause this function to trigger // Do something with response data return response; }, function (error) { // Any status codes that falls outside the range of 2xx cause this function to trigger // Do something with response error returnPromise.reject(error); });
import axios from"axios"; //要导入安装的包,则直接填写包名即可 //创建实例 const httptool = axios.create({ baseURL: 'http://127.0.0.1:8000/',//请求的公共路径,一般填写服务器api地址 timeout: 1000, //最大请求超时时间,请求超过这个时间就报错 headers: {'X-Custom-Header': 'foobar'} //预设的请求头,一般工作当中这里填写隐藏了客户端身份字段 }); // 添加请求拦截器 httptool.interceptors.request.use(function (config) { // Do something before request is sent console.log('config') console.log(config) console.log("Http请求之前") return config; }, function (error) { // Do something with request error console.log("Http请求错误") returnPromise.reject(error); });
// 添加响应拦截器 httptool.interceptors.response.use(function (response) { // Any status code that lie within the range of 2xx cause this function to trigger // Do something with response data console.log('response') console.log(response) console.log("Http响应之前") return response; }, function (error) { // Any status codes that falls outside the range of 2xx cause this function to trigger // Do something with response error console.log("Http响应错误") returnPromise.reject(error); });