跨域问题的解决

跨域问题

了解跨域问题

处于安全考虑,浏览器要求你的网站不能执行/访问其他网站的资源

跨域的影响

  • 浏览器本地存储无法使用
  • DOM对象,JS对象无法获取
  • AJAX无法使用

跨域问题的解决

  • 利用HTML标签的特性 script,a,iframe,img,link等
  • 在前端使用代理解决
  • 在服务器段解决
    CORS - 跨域资源共享,通过添加响应头信息来解决

在Vue.js中解决

1. porxy代理
新建vue.config.js
// Node.js里面的模块
module.exports = {
    // http://localhost:8080/api/test
    // =>
    // http://localhost:8000/test
  
    devServer: {
      proxy: {
        '/api': {
          // target: 'http://localhost:8000',
          target: 'http://django.t.mukewang.com',
          changeOrigin: true,
          pathRewrite: { 
            // '^/api': ''   //需要rewrite重写的URL
          },
          '/test': {
            // target: 'http://localhost:8000',
            target: 'http://localhost:9527',
            changeOrigin: true,
            pathRewrite: { 
              '^/test': ''   //需要rewrite重写的URL
            }
          }
        }
      }
    }
  }

2. 通过axios来实现发送访问:

在main.js中导入已安装好的axios,并挂载到原型上

import { ajax } from '@/utils/ajax'

export default {
  name: 'Home',
  components: {
  },
  created () {
    // ajax.get('http://django.t.mukewang.com/api/system/slider/list/?types=11')
    ajax.get('http://localhost:8080/api/system/slider/list/?types=11').then(resp => {
      console.log('返回的结果:', resp)
    }).catch(err => {
      console.log('请求出错:', err)
    })
  }
}

在Django中解决跨域问题

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

RockyBlog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值