vue javascript 跨域

vue-jsonp 解决跨域

注:此方法只支持GET请求,不支持POST
npm install vue-jsonp --save
//在main.js中
import VueJsonp from 'vue-jsonp'
Vue.use(VueJsonp)
//在需要的页面
this.$jsonp('url', {"参数,根据自己需求"}
	).then(res => {
        console.log(res)
    })
	.catch(err => {
        console.log(err)
    })
如果出现 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://xxxxxxxxx with MIME type application/javascript. See https://www.chromestatus.com/feature/5629709824032768 for more details. 这种提示,实际上是禁止跨域请求是浏览器本身的一种安全策略
跨域资源共享(CORS)机制,是为了浏览器能更为安全的处理跨域请求,使其不受同源策略的限制。简单来说就是把需要允许跨域的源写入response头里的Access-Control-Allow-Origin。对应的源就可以实现跨域资源共享了。
我们可以使用代理来解决这个问题
// 在vue.config.js中写入

module.exports = {
  devServer: {
    port: "端口号",
    proxy: {
      '/api': {
        target: '这里写你要请求的接口的固定地址',
        ws: true,
        pathRewrite: {
          '^/api': '/'  //这边用的是api  可以随便写  注意:要在请求的接口前面加上/api
        }
      }
    }
  }
}

// 例如:
this.$jsonp('/api/url', {})
	.then(res => {
        console.log(res)
    })
	.catch(err => {
        console.log(err)
    })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值