axios跨域请求

第1步:项目根目录下创建vue.config.js文件,内容如下

const path = require('path');

const resolve = dir => {

  return path.join(__dirname, dir)

};

const serverProxy = {

  '/api': {

    target: 'http://139.155.114.8:20145',

    ws: true,

    changeOrigin: true,

    pathRewrite: {

      '^/api': ' '  //通过pathRewrite重写地址,将前缀/api_screen转为/

    }

  }

};

module.exports = {

    publicPath: process.env.NODE_ENV === 'production'

    ? './'

    : '/',

    lintOnSave: false,

    // 这里写你调用接口的基础路径,来解决跨域,如果设置了代理,那你本地开发环境的axios的baseUrl要写为 '' ,即空字符串

    devServer: {

      host:'0.0.0.0',

      proxy: serverProxy

    }

}

 

第2步:main.js文件

import Axios from 'axios';

Vue.config.productionTip = false;

Vue.prototype.$axios = Axios;

Axios.defaults.baseURL = '/api';

 

第3步:this.$axios.post('接口地址', 参数(json格式), config)

this.$axios.post('/InterfaceSrv.asmx/ElecInterface', {"IN":[{"TradeID":"A001","UnitID":"1","DevID":"1","DevPsw":"123456","OpAccount":this.textVal,"OpPsw":this.passWordVal,"HappenTime":this.currentTime,"SignIn":"5FD86E9AD5CEDC6E45551E2E2057B14C145CF525AE1EF1FD220D1FC7DBC8184EBAAADFB56A574CDA"}]}, {headers:{'Content-Type':'application/x-www-form-urlencoded'}}).then((res) => {

        console.info(res);

      }).catch((err) => {

        console.info(err);

      });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值