Vue.js使用-ajax使用

1.为什么要使用ajax

前面的例子,使用的是本地模拟数据,通过ajax请求服务器数据。

2.使用jquery的ajax库示例

new Vue({
el: '#app',
data: {
searchQuery: '',
columns: [{name: 'name', iskey: true}, {name: 'age'},{name: 'sex', dataSource:['Male', 'Female']}],
peoples: []
},
ready: function () {
this.getPeoples();
},
methods: {
getPeoples: function () {
var vm = this;
$.ajax({
url: 'http://localhost:20000/my_test',
type: 'get',
dataType: 'json',
success: function (data) {
vm.$set('peoples', data.result);
},
error: function(xhr, errorType, error) {
alert('Ajax request error, errorType: ' + errorType + ', error: ' + error)
}
});
}
}
})

3.vue-resource库

vue是基于数据驱动的,不需要直接操作DOM,因此没有必要引入jquery

vue提供了一款轻量的http请求库,vue-resource

vue-resource除了提供http请求外,还提供了inteceptor拦截器功能,在访问前,访问后,做处理。

4.vue-resource语法-使用$http对象

// 基于全局Vue对象使用http
Vue.http.get('/someUrl',[options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl',[body],[options]).then(successCallback, errorCallback);
// 在一个Vue实例内使用$http
this.$http.get('/someUrl',[options]).then(sucessCallback, errorCallback);
this.$http.post('/someUrl',[body],[options]).then(successCallback, errorCallback);

then方法的回调函数写法有两种,第一种是传统的函数写法,第二种是更简洁的Lambda表达式写法。

//传统写法
this.$http.get('/someUrl',[options]).then(function(response){
//响应成功回调
},function(response){
//响应错误回调
}); //Lambda写法
this.$http.get('someUrl',[options]).then((response)=>{
//响应成功回调
},(response)=>{
//响应错误回调
});

5.vue-resource示例

<script src="js/vue-resource.js"></script>
<script>
new Vue({
el: '#app',
data: {
searchQuery: '',
columns: [{name: 'name', iskey: true}, {name: 'age'},{name: 'sex', dataSource:['Male', 'Female']}],
peoples: []
},
ready: function () {
this.getPeoples();
},
methods: {
getPeoples: function () {
var vm = this;
vm.$http.get('http://localhost:20000/my_test').then(
function (data) {
var newdata = JSON.parse(data.body)
vm.$set('peoples', newdata.result)
}).catch(function (response) {
console.log(response)
})
}
}
})
</script>

6.vue-resource用法-使用$resource对象

除了使用$http对象访问http,还可以使用$resource对象来访问。

resource服务默认提供以下几种action:

get:{method: 'GET'},

save:{method: 'POST'},

query:{method: 'GET'},

update:{method: 'PUT'},

remove:{method: 'DELETE'},

delete:{method: 'DELETE'},

resource对象使用示例如下:

new Vue({
el: '#app',
data: {
searchQuery: '',
columns: [{name: 'name', iskey: true}, {name: 'age'},{name: 'sex', dataSource:['Male', 'Female']}],
peoples: []
},
ready: function () {
this.getPeoples();
},
methods: {
getPeoples: function () {
var vm = this;
var resource = this.$resource('http://localhost:20000/my_test') resource.get().then(
function (data) {
var newdata = JSON.parse(data.body)
vm.$set('peoples', newdata.result)
}).catch(function (response) {
console.log(response)
})
}
}
})

7.拦截器interceptor

语法如下:

Vue.http.interceptors.push(function(request, next){
//...
//请求发送前的处理逻辑
//...
next(function(response){
//...
//请求发送后的处理逻辑
//...
//根据请求的状态,response参数会返回给successCallback或errorCallback
return response
}) })

8.拦截器interceptor使用示例

<div id="help">
<loading v-show="showLoading"></loading>
</div>
<template id="loading-template">
<div class="loading-overlay">
<div class="sk-three-bounce">
<div class="sk-child sk-bounce1"></div>
<div class="sk-child sk-bounce2"></div>
<div class="sk-child sk-bounce3"></div>
</div>
</div>
</template>
<script>
var help = new Vue({
el: '#help',
data: {
showLoading: false
},
components: {
'loading': {
template: '#loading-template'
}
}
}) Vue.http.interceptors.push(function(request, next){
help.showLoading = true
next(function (response) {
help.showLoading = false
return response
})
})
</script>

9.vue-resource的优点

vue-resource比jquery轻量,可以使用Vue.http或者Vue.resource处理HTTP请求,两者没有什么区别。

另外可以是用interceptor在请求前和请求后附加一些行为。

Vue.js使用-http请求的更多相关文章

  1. Vue.js之Ajax请求

    Vue.js同React.Angular,可以说号称前端三巨头. 前段时间,有个哥们说,Vue.js现在出2.0版本了.可是我现在还是在用1.0的. Vue.js一直都没有好好系统的学习,包括目前公司 ...

  2. vue.js中ajax请求

    <div id="app"> <table style="border-collapse: collapse"> <thead&g ...

  3. vue.js遍历ajax请求的数据

    <div id="dv" style="text-align: center;"><div class="head input-gr ...

  4. 基于vue.js实现远程请求json的select控件

    基本思路 前端把需要的参数类型编码传到后台,后台返回相应的参数列表json,前端利用vue渲染select控件 具体实现 前端代码 <select v-model="template. ...

  5. leyou_04_vue.js的ajax请求方式

    1.异步查询数据,自然是通过ajax查询,大家首先想起的肯定是jQuery.但jQuery与MVVM的思想不吻合,而且ajax只是jQuery的一小部分.因此不可能为了发起ajax请求而去引用这么大的 ...

  6. vue.js 请求数据

    VUE.JS var vm = new Vue({ el:"#list", data:{ gridData: "", }, mounted: function( ...

  7. 从零开始学 Web 之 Vue.js(四)Vue的Ajax请求和跨域

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  8. Vue.js 2.0 跨域请求数据

    Vuejs由1.0更新到了2.0版本.HTTP请求官方也从推荐使用Vue-Resoure变为了 axios .接下来我们来简单地用axios进行一下异步请求.(阅读本文作者默认读者具有使用npm命令的 ...

  9. vue.js学习之 跨域请求代理与axios传参

    vue.js学习之 跨域请求代理与axios传参 一:跨域请求代理 1:打开config/index.js module.exports{ dev: { } } 在这里面找到proxyTable{}, ...

随机推荐

  1. Linux 中的网络数据包捕获

    Linux 中的网络数据包捕获 Ashish Chaurasia, 工程师 简介: 本教程介绍了捕获和操纵数据包的不同机制.安全应用程序,如 VPN.防火墙和嗅探器,以及网络应用程序,如路由程序,都依 ...

  2. iOS 瀑布流之栅格布局

    代码地址如下:http://www.demodashi.com/demo/14760.html 一 .效果预览 二.确定需求 由下面的需求示意图可知模块的最小单位是正方形,边长是屏幕宽除去边距间隔后的 ...

  3. Ubuntu 12.10安装QQ2012

    [日期:2012-11-05] 在最新的Ubuntu 12.10下安装QQ2012,请根据自己的机器类型下载后按照下面的32位或64位安装说明安装. 下载网址:http://www.longene.o ...

  4. iOS刻度尺换算之1mm等于多少像素理解

    刚好看到一个刻度尺文章,实现手机屏幕上画刻度尺. 然后就有一个疑问:这个现实中的1mm(1毫米)长度与手机像素之间的换算比怎么来的呢? 看了下demo代码,发现这样写的: CGFloat sc_w = ...

  5. 从零实现Lumen-JWT扩展包(序):前因

    转自:https://zhuanlan.zhihu.com/p/22531819?refer=lsxiao 最近这段时间我寻思着把几个月前爬下来的6万多首诗词曲文做成一个API,免费开放给大家用. 这 ...

  6. IP概念盛行的背后:资本在狂欢,电影想哭泣 IP,英文“Intellectual Property”的缩写,直译为“知识产权”。它的存在方式很多元,可以是一个故事,也可以是某一个形象,运营成功的IP可以在漫画、小说、电影、玩具、手游等不同的媒介形式中转换。

    IP概念盛行的背后:资本在狂欢,电影想哭泣 IP容易拉投资.谈合作,甚至还能简化宣发途径,越来越多的人涌入了电影这个产业,争抢IP成为他们进入行业的最快捷的方法.IP盛行暴露出的另一个问题是国产电影原 ...

  7. 进阶之路(基础篇) - 008 SPI数据传输(库函数方法)

    主机端: /********************************* 代码功能:SPI数据传输(主机端) 引脚说明: SS/CS:片选(高电平屏蔽,低电平启用) MOSI :主机送出信号 M ...

  8. Rplidar学习(二)—— SDK库文件学习

    SDK头文件介绍 1.头文件简介: rplidar.h //一般情况下开发的项目中仅需要引入该头文件即可使用 RPLIDAR SDK 的所有功能. rplidar_driver.h //定义了 SDK ...

  9. HDU 1710 Binary Tree Traversals (二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  10. C# WinForm开发系列 - 文章索引

    该系列主要整理收集在使用C#开发WinForm应用文章及相关代码, 平时看到大家主要使用C#来开发Asp.Net应用,这方面的文章也特别多,而关于WinForm的文章相对少很多,而自己对WinForm ...