从实例入手学习使用vue+axios配置代理进行跨域访问百度音乐API
场景
如果要使用axios直接进行跨域访问是不可以的,这是就需要配置代理了,为什么要配置代理呢?
原因就是因为客户端请求服务端的数据是存在跨域问题的,而服务器和服务器之间可以相互请求数据,是没有跨域的概念(如果服务器没有设置禁止跨域的权限问题),也就是说,我们可以配置一个代理的服务器可以请求另一个服务器中的数据,然后把请求出来的数据返回到我们的代理服务器中,代理服务器再返回数据给我们的客户端,这样我们就可以实现跨域访问数据啦。
效果
实现
以访问百度音乐API为例
本地代理配置
打开config下的index.js
proxyTable: {"/baidu_music_api": {target: "http://tingapi.ting.baidu.com",changeOrigin: true,pathRewrite: {'^/baidu_music_api': ''}}},
注:
target:要请求的第三方平接口,这里是百度音乐API :http://tingapi.ting.baidu.com/v1/restserver/ting
changeOrigin: true
在本地会创建一个虚拟服务端,然后发送请求的数据,并同时接收请求的数据,这样服务端和服务端进行数据的交互就不会有跨域问题。
pathRewrite:路径重写
替换target中的请求地址,即别名。
安装axios
到项目根目录下,打开命令行窗口,输入:
npm install --save axios
然后重启项目
入口文件main.js中引入axios
在项目中找到src下的main.js(入口文件)打开
import Vue from 'vue'
import App from './App'
import router from './router'
import Axios from "axios"Vue.prototype.$axios = Axios;
Vue.prototype.HOST = "/baidu_music_api"
Vue.config.productionTip = false/* eslint-disable no-new */
new Vue({el: '#app',router,components: { App },template: '<App/>'
})
注:
import Axios from "axios" 引入axios
Vue.prototype.$axios = Axios; 在vue中使用axios,即挂载axios
跨域访问请求数据
在要实现访问的页面,假如加载完就要获取数据,在,mounted中:
mounted(){var url = this.HOST + "/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+ this.type +"&size=6&offset=0";this.$axios.get(url).then(res => {this.todayRecommend = res.data.song_list}).catch(error => {console.log(error);})}
将获取的数据获取存放,然后遍历获取并显示数据
完整vue代码:
<template lang="html"><div class="mod-albums"><div class="hd log url"><h2>{{title}}</h2><router-link :to="{ name:'MoreList',params:{musictype:this.type,title:title}}" tag="div">更多</router-link></div><div class="container"><div class="gallery"><div class="scroller"><div class="card url" v-for="(item,index) in todayRecommend" :key="index"><div class="album"><img :src="item.pic_big" :alt="item.title"><div class="name">{{ item.title }}</div></div></div></div></div></div></div>
</template><script>
export default {name:"todayRecommend",data(){return{todayRecommend:[]}},props:{title:{type:String,default:"今日榜单"},type:{type:String,default:"1"}},mounted(){var url = this.HOST + "/v1/restserver/ting?method=baidu.ting.billboard.billList&type="+ this.type +"&size=6&offset=0";this.$axios.get(url).then(res => {this.todayRecommend = res.data.song_list}).catch(error => {console.log(error);})}
}
</script><style scoped>.mod-albums {background-color: #fff;padding: 10px 17px;
}.hd {display: flex;margin: 14px 0 18px 0;
}.hd h2 {-webkit-box-flex: 1;-webkit-flex: 1;flex: 1;margin: 0;padding: 0;font-size: 20px;
}.hd div {width: 64px;font-size: 12px;text-align: right;
}.mod-albums .gallery {overflow: hidden;margin: 0 -5px;
}.mod-albums .gallery .card {width: 33.3%;float: left;-webkit-box-sizing: border-box;box-sizing: border-box;padding: 0 5px 10px;
}.mod-albums .gallery .card .album {position: relative;
}.mod-albums .gallery .card img {width: 100%;height: auto;border: 1px solid #eee;
}.mod-albums .gallery .card .name {font-size: 12px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;margin-top: 4px;line-height: 14px;max-height: 28px;margin-bottom: 2px;
}</style>
请求数据效果
从实例入手学习使用vue+axios配置代理进行跨域访问百度音乐API相关推荐
- vue cli配置代理解决跨域问题
内容 跨域问题 解决跨域问题 vue cli配置代理 跨域问题 是由于违背了同源策略,同源策略规定了协议名.主机名.端口号必须一致 我们目前所处的位置是http localhost 8080,我们想向 ...
- Vue之通过代理设置跨域访问
我们使用脚手架创建的项目,可以在config项目中看到 其中我们打开index.js文件 'use strict' // Template version: 1.3.1 // see http://v ...
- Vue实现仿音乐播放器11-实现访问百度音乐API实现播放音乐功能
场景 从今日推荐页面点击某首歌曲会将这首歌曲的songid通过路由传递到播放界面, 播放界面调用百度音乐接口实现数据的获取以及音乐的播放. 效果 实现 配置路由 首先在pages目录下新建musicp ...
- Vue实现仿音乐播放器5-实现今日推荐访问百度音乐API获取数据
前文 前面已经实现了音乐导航菜单的切换,现在要实现主页面的今日推荐功能. 效果 实现 实现首页重定向 当应用初始化时,默认进入主页面home页,所以在router下的index.js下配置: expo ...
- Vue实现访问百度音乐API实现播放音乐功能
场景 从今日推荐页面点击某首歌曲会将这首歌曲的songid通过路由传递到播放界面, 播放界面调用百度音乐接口实现数据的获取以及音乐的播放. 效果 实现 配置路由 首先在pages目录下新建musicp ...
- No ‘Access-Control-Allow-Origin‘ header is present on the requested resource Vue配置代理解决跨域问题
前言 在进行web开发进行数据请求的时候常常会遇到跨域问题导致无法请求数据. 如果采用如下方法向http://localhost:4000服务器发送getStudents进行接口数据请求,在后端没有处 ...
- react脚手架配置代理解决跨域问题
一.问题描述: 控制台报错,出现跨域问题 二.解决方案 配置代理: 第一种配置方式: 在package.json中追加如下配置 "proxy":"http://local ...
- react 访问后端_react前端用nginx怎么配置跨域访问后端restful api?
开发机器是windows,windows中安装了centos虚拟机做restful api服务器,在浏览器中可以正常访问该api服务器: http://192.168.33.3:8080/articl ...
- vue如何通过Nginx代理做到跨域访问API接口 解决404报错
最近和vue对接接口发现总是访问不到 404 发现原因是 vue 使用代理 访问接口的时候前面加了一层 例如 访问 api/index/index 实际是 api/api/index/index 找到 ...
最新文章
- python 判断字典是否包含某个key,以及对应的value 值
- JavaScript语言基础.上
- sql server与java实例_Origin数据处理实例教程50节02040101
- NLP多任务学习:一种层次增长的神经网络结构 | PaperDaily #16
- 17/100. Maximum Subarray
- Gray Code(格雷码) C++多方法实现
- 华为机试HJ46:截取字符串
- 在hbase 激活kerberos 下opentsdb的使用
- WindowsServer2008R2系统版本升级
- 计算机上图片打不开,电脑上打不开jpg格式的图片是怎么回事?
- Cesium 加载地形数据
- java实现生命游戏(2维元胞自动机)
- day3-转自金角大王
- python大数据运维常用脚本_python大数据运维
- seurat使用笔记(数据处理、PCA、聚类)
- 找到读取文本文件的方法
- PyQt5-五十音图听写
- Altium 常用快捷键
- 如何安装seed_ubantu20.04
- matlab simca,SIMCA-P+软件
热门文章
- J.U.C系列(三)Semaphore的使用
- 虚拟机(VMware Workstation Pro)安装多台Linux
- 系统幂等性设计与实践
- copyproperties爆红_BeanUtils.copyProperties复制失败探究
- b区计算机复试国家线,考研国家线/自主划线/a区b区线/专业线这些考研复试分数线你能分清吗?...
- html全局布局 vue_基于Vue+Element的电商后台管理系统
- i.MX 系列CPU HAB漏洞SecureBoot漏洞
- 百度云获取外链直接下载突破限速
- php 控件 手机触控,html5手机触屏touch事件的详细介绍
- redis介绍以及使用 ppt_光纤收发器的工作原理以及使用方法介绍