vue-cli使用axios配置跨域

àì夳堔傛蜴生んèń 2022-04-23 06:44 338阅读 0赞

axios.js配置

  1. import axios from 'axios';
  2. import qs from "qs";
  3. axios.defaults.timeout = 5000;
  4. axios.defaults.baseURL = 'http://192.168.1.114:5418/api'; //本地端口和地址
  5. axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';// 配置请求头
  6. axios.interceptors.request.use((config) => {
  7. //请求参数转成字符串
  8. config.data = qs.stringify(config.data, {
  9. allowDots: false //Option allowDots can be used to enable dot notation
  10. });
  11. return config;
  12. }, (error) => Promise.reject(error));
  13. export default axios;

配置文件config/index.js

  1. const path = require('path')
  2. module.exports = {
  3. ...
  4. proxyTable: {
  5. '/api': {
  6. target: 'http://192.168.1.54:10086/v1', //跨域访问的接口地址
  7. pathRewrite: {
  8. '^/api': ''
  9. }
  10. }
  11. },
  12. ...
  13. }

使用

  1. import axios from './plugin/axios'
  2. axios.get('/public_activity/get_status?id=15')
  3. .then((response) => {
  4. console.log(response);
  5. })
  6. .catch((error) => {
  7. console.log(error);
  8. });

发表评论

表情:
评论列表 (有 0 条评论,338人围观)

还没有评论,来说两句吧...

相关阅读