zoukankan      html  css  js  c++  java
  • vue+axios跨域解决方法

    通过这种方法也可以解决跨域的问题。

    使用http-proxy-middleware 代理解决(项目使用vue-cli脚手架搭建)

    例如请求的url:“http://f.apiplus.cn/bj11x5.json

    1、打开config/index.js,在proxyTable中添写如下代码:

         

    1
    2
    3
    4
    5
    6
    7
    8
    9
    proxyTable: { 
      '/api': {  //使用"/api"来代替"http://f.apiplus.c" 
        target: 'http://f.apiplus.cn', //源地址 
        changeOrigin: true, //改变源 
        pathRewrite: { 
          '^/api': 'http://f.apiplus.cn' //路径重写 
          
      
    }

    2、使用axios请求数据时直接使用“/api”:

    1
    2
    3
    4
    getData () { 
     axios.get('/api/bj11x5.json', function (res) { 
       console.log(res) 
     })

    通过这中方法去解决跨域,打包部署时还按这种方法会出问题。解决方法如下:

    1
    2
    3
    4
    5
    let serverUrl = '/api/'  //本地调试时 
    // let serverUrl = 'http://f.apiplus.cn/'  //打包部署上线时 
    export default { 
      dataUrl: serverUrl + 'bj11x5.json' 
    }
    调试时定义一个serverUrl来替换我们的“/api”,最后打包时,只需要将“http://www.xxx.com”替换这个“/api”就可以了。
     
    原网址:https://www.cnblogs.com/wangyongcun/p/7665687.html
  • 相关阅读:
    IOS 模块并且发布到NPM
    处理npm publish报错问题
    IOS 应用发布流程
    《温故而知新》JAVA基础八
    Beta冲刺 (5/7)
    Beta冲刺 (4/7)
    Beta冲刺 (3/7)
    Beta冲刺 (2/7)
    软件测试(团队)
    福大软工 · BETA 版冲刺前准备(团队)
  • 原文地址:https://www.cnblogs.com/stylesu/p/9100265.html
Copyright © 2011-2022 走看看