axios实现跨域分别是哪三种方法


本文摘自PHP中文网,作者coldplay.xixi,侵删。

axios实现跨域的三种方法:1、在【mian.js】中引用axios,代码为【Vue.prototype.$axios = axios】;2、在页面中引用axios,代码为【this.$axios.post('/api/】。

axios实现跨域的三种方法:

1. mian.js 引用axios

1

2

3

import axios from 'axios'

  

Vue.prototype.$axios = axios

2.config/index.js 的dev中添加如下代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

proxyTable: {

  

    '/api': {

  

         target: 'http://127.0.0.1/hyhy/',//设置你调用的接口域名和端口号 别忘了加http

  

         changeOrigin: true,

  

         pathRewrite: {

  

              '^/api': '/'//这里理解成用‘/api’代替target里面的地址,后面组件中我们掉接口时直接用api代替 比如我要调             用'http://40.00.100.100:3002/user/add',直接写‘/api/user/add’即可

  

        }

  

    }

  

}

3.在页面中引用

1

2

3

4

5

6

7

8

9

10

11

12

13

this.$axios.post('/api/userlogin/login', Qs.stringify({ username: this.username, password: this.password }))

  

.then((response) => {

  

    console.log(response)

  

})

  

.catch((response) => {

  

    console.log(response)

  

})

相关学习推荐:js视频教程

以上就是axios实现跨域分别是哪三种方法的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

vue+axios+php如何实现上传文件功能?

讨论react axios 跨域访问一个或多个域名问题

详解websocket跨域问题解决

node.js设置允许跨域的方法

ajax跨域的基本流程

浅谈vue中axios的封装

axios实现跨域分别是哪三种方法

更多相关阅读请进入《axios》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...