axios跨域的方法有哪些

2023-05-16,,

这篇文章将为大家详细讲解有关axios跨域的方法有哪些,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

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

axios实现跨域的三种方法:

1. mian.js 引用axios

import axios from 'axios'
 
Vue.prototype.$axios = axios

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

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.在页面中引用

this.$axios.post('/api/userlogin/login', Qs.stringify({ username: this.username, password: this.password }))
 
.then((response) => {
 
    console.log(response)
 
})
 
.catch((response) => {
 
    console.log(response)
 
})

关于axios跨域的方法有哪些就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

《axios跨域的方法有哪些.doc》

下载本文的Word格式文档,以方便收藏与打印。