axios实现跨域及突破host和referer的限制

跨域请求设置

以qq音乐为例

  1. 配置vue.config.js
   devServer: {
    proxy: { // 代理跨域
      '/api': { // 代理url关键字
        target: 'https://c.y.qq.com', // 需要代理的地址
        secure: false, // 如果是https接口,需要配置这个参数
        changeOrigin: true, // 是否跨域
        pathRewrite: {
          '^/api': ''
        }
      },
      '/pc': { // 代理url关键字
        target: 'https://u.y.qq.com', // 需要代理的地址
        secure: false, // 如果是https接口,需要配置这个参数
        changeOrigin: true, // 是否跨域
        pathRewrite: {
          '^/pc': ''
        },
        // 突破host和origin的限制
        headers: {
          referer: 'https://y.qq.com/',
          origin: 'https://y.qq.com'
        }
      }
}
  1. ajax请求
import axios from 'axios'
// 获取轮播图数据
export function banner () {
  return axios({
    method: 'get',
    url: '/api/musichall/fcgi-bin/fcg_yqqhomepagerecommend.fcg',
    params: {
      // 参数
    }
  })
}
// 获取歌曲列表
export function songList () {
  const url = '/pc/cgi-bin/musicu.fcg'
  const data = Object.assign({}, commonParams, {
    loginUin: 0,
    hostUin: 0,
    platform: 'yqq.json',
    needNewCode: 0,
    data: JSON.stringify({
      'comm': { 'ct': 24 },
      'playlist': {
        'method': 'get_playlist_by_category',
        'param': {
          'id': 3317,
          'curPage': 1,
          'size': 40,
          'order': 5,
          'titleid': 3317
        },
        'module': 'playlist.PlayListPlazaServer'
      }
    }),
    ...commonParams
  })
  return axios({
    method: 'get',
    url: url,
    params: data
  })
}

注意请求的路径后面跟的参数一定要用params传递

  1. 在需要的组件内调用
import { banner, songList } from '../../api/store'
export default {
  data () {
    return {
      bannerList: [],
      songList: []
    }
  },
  created () {
    this.getBanner()
    this.getSongList()
  },
  methods: {
    getBanner () {
      banner().then(res => {
        if (res.status === 200) {
          this.bannerList = res.data.data.slider
        }
      })
    },
    getSongList () {
      songList().then(res => {
        if (res.status === 200) {
          this.songList = res
        }
      })
    }
  }
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Axios是一个基于Promise的HTTP客户端,可以用于浏览器和Node.js。Vue中使用Axios可以方便地进行HTTP请求。在Vue中使用Axios进行跨域请求,可以通过以下两种方式解决: 1. 在Vue中进行配置 可以在Vue的配置文件中设置Axios的默认配置,包括跨域请求的相关配置。具体步骤如下: ① 安装Axios ```shell npm install axios --save ``` ② 在main.js中引入Axios并进行配置 ```javascript import axios from 'axios' // 设置Axios的默认配置 axios.defaults.baseURL = 'http://localhost:8080' // 设置请求的基础URL axios.defaults.timeout = 5000 // 设置请求超时时间 // 设置Axios跨域请求相关配置 axios.defaults.withCredentials = true // 允许携带cookie axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded' // 设置POST请求的请求头 // 将Axios挂载到Vue的原型上,方便在组件中使用 Vue.prototype.$axios = axios ``` ③ 在组件中使用Axios进行跨域请求 ```javascript this.$axios.get('/api/user', { params: { id: 1 } }).then(res => { console.log(res.data) }).catch(err => { console.log(err) }) ``` 2. 使用Nginx转发 可以使用Nginx作为反向代理服务器,将Vue的请求转发到后端服务器上,从而实现跨域请求。具体步骤如下: ① 安装Nginx ```shell sudo apt-get install nginx ``` ② 配置Nginx 在Nginx的配置文件中添加以下内容: ```nginx server { listen 80; server_name localhost; location /api { proxy_pass http://localhost:8080; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } ``` 其中,`/api`是Vue的请求路径,`http://localhost:8080`是后端服务器的地址。 ③ 在组件中使用Axios进行跨域请求 ```javascript this.$axios.get('/api/user', { params: { id: 1 } }).then(res => { console.log(res.data) }).catch(err => { console.log(err) }) ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值