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

这三种建议使用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. LeetCode Day 8

    LeetCode0015 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 例如, 给 ...

  2. Django环境的搭建以及最简示例

    一.环境的搭建 先安装pip yum install python-pip 安装失败: 安装epel扩展源 yum install epel-release 在安装pip 再利用pip安装django ...

  3. 反编译.net下的exe程序

    1. 什么叫.net平台 .NET框架是一个多语言组件开发和执行环境,它提供了一个跨语言的统一编程环境..NET框架的目的是便于开发人员更容易地建立Web应用程序和Web服务,使得Internet上的 ...

  4. 【React.js小书】动手实现 React-redux(五):Provider - 方志

    我们要把 context 相关的代码从所有业务组件中清除出去,现在的代码里面还有一个地方是被污染的.那就是 src/index.js 里面的 Index: 1234567891011121314151 ...

  5. 吴裕雄--天生自然 R语言开发学习:基本数据管理

    #---------------------------------------------------------# # R in Action (2nd ed): Chapter 4 # # Ba ...

  6. Vue错误信息解决

    在运行Vue项目时提示如下错误: [Vue warn]: You are using the runtime-only build of Vue where the template compiler ...

  7. IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml

    IDEA工具java.io.IOException: Could not find resource SqlMapConfig.xml 解决办法: 1.删掉pom.xml文件的这行代码 <pac ...

  8. vue watch和computed的使用场景

    watch 监听某个数据的变化(监听完调用什么函数) 一个数据影响多个数据 (比如:浏览器自适应.监控路由对象.监控自身属性变化) computed 计算后返回新 一个数据受多个数据影响(比如:计算总 ...

  9. 【转】从技术和成本算笔账,自动驾驶L3过渡到L4有多难?

    转自:http://www.sohu.com/a/160479216_121787 从技术和成本算笔账,自动驾驶L3过渡到L4有多难? 2017-07-28 09:34 英伟达解决方案架构师程亚冰认为 ...

  10. Spring Security基本原理

    近期研究了Spring Security,现进行记录. 首先先进行一个最简单的demo.默认情况下,在Spring Boot里,如果在classpath下面有Spring Security相关的jar ...