vue中数据请求的三种方法
注意请求可能存在跨域问题,需要去配置好
这三种建议使用axios
1.resource
Vue 要实现异步加载需要使用到 vue-resource 库。
Vue.js 2.0 版本推荐使用 axios 来完成 ajax 请求。
先导入一个线上cdn的地址,当然还可以去npm安装,但个人觉得不做项目的话使用这种测试方便
<script src="https://cdn.staticfile.org/vue-resource/1.5.1/vue-resource.min.js"></script>
实现GET请求
<div id="box">
<ul>
<li v-for='item of img'><img :src='item.img' alt=""></li>
</ul>
</div>
<script>
var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted() {
//get请求
this.$http.get('http://localhost:3000/api/banner').then(function(res){
this.img = res.body.data
},function(){
console.log('请求失败处理');
});
}
})
</script>
如果需要传递数据,可以使用 this.$http.get('get.php',{params : jsonData}) 格式,第二个参数 jsonData 就是传到后端的数据。
实现POST请求
<div id="box">
<ul>
<li v-for='item of img'><img :src='item.img' alt=""></li>
</ul>
</div>
<script>
var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted() {
//post请求 需要第三个参数{emulateJSON:true}
this.$http.get('http://localhost:3000/api/banner',{name: '王富贵'},{emulateJSON:true}).then(function(res){
this.img = res.body.data
},function(){
console.log('请求失败处理');
});
}
})
post 发送数据到后端,需要第三个参数 {emulateJSON:true}。
emulateJSON 的作用: 如果Web服务器无法处理编码为 application/json 的请求,你可以启用 emulateJSON 选项。
2.fetch(次方法vue自带的不需要安装其他)
GET方法
这个方法只能在地址栏传参
//get方法 只能在地址栏传参
fetch('http://localhost:3000/api/banner')
.then(res => {
//return 返回给上一层 ,不然下一层用不了
return res.json()
})
.then(data => {
console.log(data)
})
POST方法
var url = 'http://localhost:3000/api/user/loging'
fetch(url, {
method: 'post',
headers: {
//这里是需要去network里面看
'Content-Type': 'application/x-www-form-urlencoded'
},
body: 'tel=xdd212&password=1515'
})
.then(res => {
//return 返回给上一层 ,不然下一层用不了
return res.json()
})
.then(data => {
console.log(data)
})
另一种传参方式
//post 另一种传参方式
fetch(url, {
method: 'post',
headers: {
//看个人习惯
'Content-Type': 'application/json'
},
body: JSON.stringify({
tel: 'xdd212',
password: '1515'
})
})
.then(res => {
//return 返回给上一层 ,不然下一层用不了
return res.json()
})
.then(data => {
console.log(data)
})
3.axios
Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中
用法和jq很类似
安装或者引入cdn地址 npm i axios
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
GET请求
<div id="box">
<ul>
<li v-for='item of img'><img :src='item.img' alt=""></li>
</ul>
</div>
<script>
var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted(){
//需要传参的话可以在地址栏后面拼接
axios.get('http://localhost:3000/api/banner',
//还可以这样传参
// {
// params:{
// name:'王富贵'
// }
// }
)
.then(res => {
console.log(res)
})
}
})
</script>
POST请求
<div id="box">
<ul>
<li v-for='item of img'><img :src='item.img' alt=""></li>
</ul>
</div>
<script>
var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted(){
//需要传参的话可以在地址栏后面拼接
axios.post('http://localhost:3000/api/urse/loging',
{
age: 18
name:'王富贵'
}
)
.then(res => {
console.log(res)
})
}
})
</script>
一次执行多个请求
var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted(){
function fn1(){
return axios.get('http://localhost:3000/api/banner')
}
function fn2(){
return axios.get('http://localhost:3000/api/pro')
}
//注意这里必须要使用数组
axios.all([fn1() , fn2()])
//函数里面对应的数字里面的值
.then(axios.spread(function (fn1, fn2) {
console.log(fn1)
}))
}
})
axios
可以自己配置参数
var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted(){
axios({
//注意不写请求方式的话默认是get
method: 'post',
url: 'http://localhost:3000/api/user/loging',
data: {
tel: "xdd212",
password: "1515"
}
})
.then(res => {
console.log(res)
})
}
})
你可以自己定义一个axios
//这里创建一个自定义配置的axios
var init = axios.create({
//这里可配置文件的长路径
baseURL: 'http://localhost:3000/api'
})
// 假设如果很多接口都需要使用 token验证,可以把token信息放在请求头
init.defaults.headers.token = '1231654984561646546565464654654646321654asdasdasd' var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted(){
//下面调用axios的时候就是调用我们自定义创建的
init({
method:'get',
//然后到这下面可以直接写短路径,后期方便维护
url: '/banner'
})
.then(res => {
console.log(res)
})
//此方法也是一样
init.get('/banner')
.then(res => {
console.log(res)
})
}
})
拦截器
请求拦截器和响应拦截器
//请求前
axios.interceptors.request.use(function (config) {
// 可以设置 加载的动画效果 的展示
//这里指的是请求之前做点什么
console.log('正在加载...')
return config
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
}) //响应前
axios.interceptors.response.use(function (config) {
// 可以设置 加载的动画效果 的隐藏
//这里指的是请求之前做点什么
console.log('加载完毕')
return config
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
}) var app = new Vue({
el: '#box',
data: {
img: ''
},
mounted(){
axios.get('http://localhost:3000/api/banner')
}
})
vue中数据请求的三种方法的更多相关文章
- php发送post请求的三种方法示例
本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...
- 【MySQL】锁——查看当前数据库锁请求的三种方法 20
MySQL提供了查看当前数据库锁请求的三种方法:1. show full processlist命令 观察state和info列 2. show engine innodb status\G ...
- jQuery中detach&&remove&&empty三种方法的区别
jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...
- IOS开发中数据持久化的几种方法--NSUserDefaults
IOS开发中数据持久化的几种方法--NSUserDefaults IOS 开发中,经常会遇到需要把一些数据保存在本地的情况,那么这个时候我们有以下几种可以选择的方案: 一.使用NSUserDefaul ...
- 三、用Delphi10.3 创建一条JSON数据的第三种方法,非常简洁的写法
一.用Delphi10.3构造一个JSON数据的第三种方法,并格式化输出,代码如下: uses // System.JSON, System.JSON.Types, System.JSON.Write ...
- Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)
Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...
- Openerp 中打开 URL 的三种 方法
来自:http://shine-it.net/index.php/topic,8013.0.html 最近总结了,Openerp 中打开 URL 的三种 方法: 一.在form view 添加 < ...
- mysql 中添加索引的三种方法
原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...
- PHP中数据类型转换的三种方式
PHP中数据类型转换的三种方式 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: 1.(int).(integer):转换成整形2.(float).(double).(real):转换成 ...
随机推荐
- VisionPro控件的使用 C# 开发篇
VisionPro 常用控件的说明 工具设置窗体 CogPMAlignEditV2 [ 模版匹配设置窗体控件 ] CogPMAlignEditV2.Subject : 工具关联对象 如:CogPMA ...
- uname|mv|tar -xzvf|
$ ls CAFE-4.2.1.tar.gz mcl-latest.tar.gz mysql-5.4.3-beta-linux-i686-glibc23.tar.gz.1 orthomclSoftwa ...
- zoj2588-tarjan求桥/割边
tarjan求桥,算法流程详见核心代码: void tarjan(int k){ dfn[k]=low[k]=++cnt; //fa[k]=(edge){f,0,fid}; for(int i=hea ...
- JavaScript学习总结(四)function函数部分
转自:http://segmentfault.com/a/1190000000660786 概念 函数是由事件驱动的或者当它被调用时执行的可重复使用的代码块. js 支持两种函数:一类是语言内部的函数 ...
- Eclipse 配置Android 开发环境
1.eclipse安装ADT插件. 地址:http://dl-ssl.google.com/android/eclipse/ 2.安装成功后,提示重启.重新启动后弹出android sdk 的路径选择 ...
- Nginx笔记总结一:基本安装和配置
1. Nginx安装 下载地址和安装依赖包 http://nginx.org/download/nginx-1.9.14.tar.gz yum -y install pcre pcre-devel z ...
- Servlet+JSP 对外访问路径配置
servlet类似 servlet配置为: <servlet> <servlet-name>Demo01_OutWrite</servlet-name> ...
- Leetcode回溯相关题目Python实现
1.46题,全排列 https://leetcode-cn.com/problems/permutations/ class Solution(object): def permute(self, n ...
- zctf 2016 android writeup - Jieming的博客
本文为2016年zctf中android的writeup. 首先点我下载题目.使用jeb反编译,对username和password进行部分验证后,再将username+password及一个数据库查 ...
- RocketMQ集群平滑下线或重启某个节点
1.现状描述 集群其中一台物理机未知原因导致单用户无法登陆机器,该物理机需要重启修改密码或者重装系统.该台为master节点,运行正常.配置策略为: 异步刷盘 主从异步复制 如果直接下线该master ...