注意请求可能存在跨域问题,需要去配置好

这三种建议使用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中数据请求的三种方法的更多相关文章

  1. php发送post请求的三种方法示例

    本文分享下php发送post请求的三种方法与示例代码,分别使用curl.file_get_content.fsocket来实现post提交数据,大家做个参考. php发送post请求的三种方法,分别使 ...

  2. 【MySQL】锁——查看当前数据库锁请求的三种方法 20

    MySQL提供了查看当前数据库锁请求的三种方法:1. show  full  processlist命令  观察state和info列 2. show engine  innodb status\G ...

  3. jQuery中detach&&remove&&empty三种方法的区别

    jQuery中empty&&remove&&detach三种方法的区别 empty():移除指定元素内部的所有内容,但不包括它本身 remove():移除指定元素内部的 ...

  4. IOS开发中数据持久化的几种方法--NSUserDefaults

    IOS开发中数据持久化的几种方法--NSUserDefaults IOS 开发中,经常会遇到需要把一些数据保存在本地的情况,那么这个时候我们有以下几种可以选择的方案: 一.使用NSUserDefaul ...

  5. 三、用Delphi10.3 创建一条JSON数据的第三种方法,非常简洁的写法

    一.用Delphi10.3构造一个JSON数据的第三种方法,并格式化输出,代码如下: uses // System.JSON, System.JSON.Types, System.JSON.Write ...

  6. Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法(转)

    Android中全屏 取消标题栏,TabHost中设置NoTitleBar的三种方法http://www.cnblogs.com/zdz8207/archive/2013/02/27/android- ...

  7. Openerp 中打开 URL 的三种 方法

    来自:http://shine-it.net/index.php/topic,8013.0.html 最近总结了,Openerp 中打开 URL 的三种 方法: 一.在form view 添加 < ...

  8. mysql 中添加索引的三种方法

    原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...

  9. PHP中数据类型转换的三种方式

    PHP中数据类型转换的三种方式 PHP的数据类型转换属于强制转换,允许转换的PHP数据类型有: 1.(int).(integer):转换成整形2.(float).(double).(real):转换成 ...

随机推荐

  1. [LC] 102. Binary Tree Level Order Traversal

    Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...

  2. 106)PHP,缩略图代码和结果展示

    首先是 代码展示: <?php class CImage { /** * 生成保持原图纵横比的缩略图,支持.png .jpg .gif * 缩略图类型统一为.png格式 *@param $src ...

  3. 对TD tree的使用体验及建议

    TDtree与QQ空间有着相似的功能,你可以在里面发表自己的感受与心情,也可以存储照片或者给喜欢的说说点赞.发表评论等,可以说这是一个将QQ空间从QQ里独立出来的软件.作为一个娱乐性的软件,它的功能还 ...

  4. Java为什么能够跨平台?

    首先介绍一下Java的各个层级,先放一张图: 硬件,操作系统和操作系统接口:这三级不说大家都知道,操作系统有很多种,比如Windows,Linux.Windows又分为win7,win10,win x ...

  5. KUKA机器人常见十大问题及解决方法

    1 开机坐标系无效 世界坐标系是以枪头为基点,在这种坐标系中,机器人所有的动作都是按照以枪头为顶点来完成移动,XYZ方向切割枪方向不改变,如果机器人在世界坐标系中移动,枪头也随着改变方向,那就是我们在 ...

  6. 微软Hyperlapse技术:让第一人称摄像稳定而流畅

    编者按:GoPro等第一人称摄像设备已经几乎成为了极限运动者的标配,但拍摄过程中的抖动常会让画面非常糟糕.微软Hyperlapse技术实现了将第一人称录像转化成稳定而流畅的视频.该成果的论文已发表在S ...

  7. 题解 HDU 3698 Let the light guide us Dp + 线段树优化

    http://acm.hdu.edu.cn/showproblem.php?pid=3698 Let the light guide us Time Limit: 5000/2000 MS (Java ...

  8. 关于使用gitlab协同开发提交代码步骤

    记录使用gitlab协同开发时从自己的分支向master分支提交代码的步骤: 环境:安装了git和TortoiseGit(git的可视化工具) 1.首先切换到自己的分支(如果不在自己的分支) 2.gi ...

  9. resin远程调试debug

    wangqiaowqo Resin 远程debug Resin Windows下提升Resin默认的虚拟机内存大小 httpd.exe -Xmx1024m 参考外部文章 Resin远程debug配置文 ...

  10. 剑指offer-18-2. 删除链表中重复的结点

    剑指offer-18-2. 删除链表中重复的结点 链表 在一个排序的链表中,存在重复的结点,请删除该链表中重复的结点,重复的结点不保留,返回链表头指针. 例如,链表1->2->3-> ...