vue2.x中的跨域

本文介绍了在Vue2.x中如何解决跨域问题,通过示例展示了使用fetch和axios进行API调用的方法。在App.vue组件中,fetch示例展示了设置请求头并发送POST请求的过程,然后转换响应为JSON数据。另外,文章还提到了axios的安装与使用,将其挂载到Vue原型上,方便全局调用。
摘要由CSDN通过智能技术生成

1、fetch
App.vue
//跨域请求处理
created(){
//fetch
fetch("/apis/server_new/login",{
method:“post”,
header:{
// “Content-type”:“application/json”,
token:“54cas56156d4a6s1das1d6sa4d6a51d65asd”
},
body:JSON.stringify({username:“admin”,password:“123456”})
})
.then(result=>{
// console.log(result)
return result.json()
})
.then(data=>{
console.log(data)
})

}



在config/index.js
 // Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: {
	'/apis': {    //将www.exaple.com印射为/apis
        target: 'https://www.exaple.com',  // 接口域名
        secure: false,  // 如果是https接口,需要配置这个参数
        changeOrigin: true,  //是否跨域
        pathRewrite: {
            '^/apis': ''   //需要rewrite重写的,
        }              
    }
},

重启:必须重启
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值