vue开发,ajax跨域解决办法

在config/index.js中配置

module.exports = {
    lintOnSave:false,
    devServer:{
        port:8081,
        proxy: {
            "/api": {
                target: "http://localhost:8080",//后端接口地址
                pathRewrite: {
                    "^/api": "/项目名"
                }
            }
        }
    }
}

在service中配置index.js

import axios from 'axios'
import Qs from 'qs'
export const register = async (//参数)=>{
    let response = await axios({
        method:'post',
        url:'/api/后端地址',
        data:Qs.stringify({//参数})
    })
    return response;
}

axios调用示例

import {register} from '../service/index'
export default {
  data(){
    return{
      phoneNum:'',
      pwd:'',
    }
  },
  methods:{
      async reg(){
        let response = await register(this.phoneNum,this.pwd);
        console.log(response)
      }
    }
  
}

  

posted @ 2020-10-25 19:04  FivePointOne  阅读(222)  评论(0编辑  收藏  举报