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
  • 相关阅读:
    python 之os模块用法大全
    web自动化测试常用的定位方式有哪些?
    Jenkins
    吴恩达机器学习笔记:如何debug一个学习算法?
    贝叶斯网络
    leetcode-32. Longest Valid Parentheses
    leetcode4 median Of two sorted array
    leetcode21
    vs2017问题集锦
    LIS
  • 原文地址:https://www.cnblogs.com/stylesu/p/9100265.html
Copyright © 2011-2022 走看看