Vue.js使用-http请求
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请求的更多相关文章
- Vue.js之Ajax请求
Vue.js同React.Angular,可以说号称前端三巨头. 前段时间,有个哥们说,Vue.js现在出2.0版本了.可是我现在还是在用1.0的. Vue.js一直都没有好好系统的学习,包括目前公司 ...
- vue.js中ajax请求
<div id="app"> <table style="border-collapse: collapse"> <thead&g ...
- vue.js遍历ajax请求的数据
<div id="dv" style="text-align: center;"><div class="head input-gr ...
- 基于vue.js实现远程请求json的select控件
基本思路 前端把需要的参数类型编码传到后台,后台返回相应的参数列表json,前端利用vue渲染select控件 具体实现 前端代码 <select v-model="template. ...
- leyou_04_vue.js的ajax请求方式
1.异步查询数据,自然是通过ajax查询,大家首先想起的肯定是jQuery.但jQuery与MVVM的思想不吻合,而且ajax只是jQuery的一小部分.因此不可能为了发起ajax请求而去引用这么大的 ...
- vue.js 请求数据
VUE.JS var vm = new Vue({ el:"#list", data:{ gridData: "", }, mounted: function( ...
- 从零开始学 Web 之 Vue.js(四)Vue的Ajax请求和跨域
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- Vue.js 2.0 跨域请求数据
Vuejs由1.0更新到了2.0版本.HTTP请求官方也从推荐使用Vue-Resoure变为了 axios .接下来我们来简单地用axios进行一下异步请求.(阅读本文作者默认读者具有使用npm命令的 ...
- vue.js学习之 跨域请求代理与axios传参
vue.js学习之 跨域请求代理与axios传参 一:跨域请求代理 1:打开config/index.js module.exports{ dev: { } } 在这里面找到proxyTable{}, ...
随机推荐
- 关于js中的回调函数callback
来源于:http://www.jianshu.com/p/6bc353e5f7a3 前言 其实我一直很困惑关于js 中的callback,困惑的原因是,学习中这块看的资料少,但是平时又经常见,偶尔复制 ...
- cnpm不是内部或外部命令 cnpm: command not found
问题是处在于 你没用用淘宝的镜像 安装cnpm 不信 你打下cnpm -v, 看是 是不是也不是内部命令: 好了,那就安装下吧 npm install cnpm -g --registry=htt ...
- Swift3.0 - 实现剪切板代码拷贝及跨应用粘贴
有个需求,点击某个按钮,实现一段内容的拷贝,然后到其他应用内,直接长按粘贴. 实现如下: /// 测试剪切板,实现代码拷贝内容 func testPasteBoard(str:String) { // ...
- 支持向量机-SVM 学习
一 .支持向量机(SVM) 1.1 符号定义 标签 y 不再取 0 或 1,而是: y∈{-1, 1} 定义函数: 向量,没有第 0 个维度,b 为截距,预测函数定义为: 1.2 函数间隔与几何间隔 ...
- JavaScript Window Location 当前页面的地址
window.location 对象用于获得当前页面的地址 (URL),并把浏览器重定向到新的页面. Window Location window.location 对象在编写时可不使用 window ...
- 【Struts2】result类型
Struts2 result类型 1.dispatcher:服务器跳转到页面,通常来处理JSP,默认类型. 2.redirect:重定向到页面. Action: 1 public String red ...
- Git 配置(分布式版本控制系统)
1.Mac Git 配置文件 既然已经在系统上安装了 Git,你会想要做几件事来定制你的 Git 环境.每台计算机上只需要配置一次,程序升级时会保留配置信息.你可以在任何时候再次通过运行命令来修改它们 ...
- log4j(六)——log4j.properties简单配置样例说明
一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 二:老规矩,先来个栗子,然后再聊聊感受 (1)使用配文件的方式,是不是感觉非常的清爽,如果不在程序中读取配置文件就更加的清 ...
- asp.net url址址中中文汉字参数传递乱码解决方法
中文乱码是网站开发中会常碰到的问题,今天我们来讲一下关于url址址中中文汉字参数传递乱码解决方法,有需要的朋友可以参考下.在cs文件里传参的时候用UrlEncode: Response.Redirec ...
- IPython介绍及安装
IPython介绍 - CSDN博客https://blog.csdn.net/gavin_john/article/details/53086766 python命令行在windows下实现tab自 ...