代替ajax操作
在mian.js里面引入 import vueResource from 'vue-resource'
mian.js会 加载全局js
下面介绍的是基于Vue实例的方式,一般项目只需基于vue实例,项目中一般只使用post和get两种请求方式

1) post请求方法
this.$http.post(API.goodsProvideInfo, {}, { params: params }).then((res) => {
// 成功
}).catch(function (response) {
// 失败
console.log(response)
})
参数说明 1. url string (请求的url)
2.body Object (request body)(一般为空)
3.params Object (请求参数)

2) get
this.$http.get(API.goodsProvideInfo, {params: {sku: 1213}}).then((res) => {
}).catch(function (response) {
console.log(response)
})
参数说明 1. url string (请求的url)
2.params Object (请求参数)(post和get的传参数位置不同)

注:post和get传参方式不一样,post第二个参数是body,第三个是params,get 第二个参数是params
then--成功回调,catch--失败回调
注:共支持7种请求方式(了解)
get(url, [options])
post(url, [body], [options])
head(url, [options])
delete(url, [options])
jsonp(url, [options])
put(url, [body], [options])
patch(url, [body], [options])
3)http 拦截

http拦截在mian.js做统一的拦截处理,有 加loading,处理返回错误,所以项目中的http请求不需要catch 方法,请求前可以对参数做统一处理,如加公共参数uid,中文encode,加密等。对请求完的数据 response做统一处理。
Vue.http.interceptors.push((request, next) => {
request.credentials = true // 这里解决跨域问题
// 请求发送前的处理逻辑
loading() // 加loading

next((response) => {
loadingHide() // 去掉loading

// 可以修改返回的response
if (response.data && response.data.code === '0') {
response.code = response.data.code
response.data = response.data.data

// 下面是统一的错误处理
} else if (response.data && response.data.code === 'TOKENINVALID_999') {
// 说明token过期,需要重新登录
window.location.href = '/login'
} else {
if (response.data && response.data.message) {
Notifiy('错误', response.data.message, 'error') // 弹出后台错误提示
} else if (response.data && response.data.code) {
response.code = response.data.code
} else {
Notifiy('错误', '请求出错', 'error')
response.code = '222222'
}
}
})
})

vue-resource的更多相关文章

  1. vue resource 携带cookie请求 vue cookie 跨域

    vue resource 携带cookie请求 vue cookie 跨域 1.依赖VueResource 确保已安装vue-resource到项目中,找到当前项目,命令行输入: npm instal ...

  2. Vue Resource root options not used?

    I specify a root options in my Vue-Resource in my main.js file, but when I do the request, it does n ...

  3. vue -resource 文件提交提示process,或者拦截处理

    this.$http.post('url',fd||data,{emulateJSON:true}).then(fn(res){},fn(res){}) process成功案例 _self.$http ...

  4. vue resource 携带cookie请求 vue cookie 跨域(六)

    1.依赖VueResource  确保已安装vue-resource到项目中,找到当前项目,命令行输入: npm install vue-resource --save 在主方法添加 过滤 Vue.h ...

  5. vue resource patch方法的传递数据 form data 为 [object Object]

    今天在测试 iblog 登录时,传送过去的数据总是 [object Object],以至于后台识别不出来. vue 使用了 vueResource 组件,登录方法为 patch. 经过探索,终于在官网 ...

  6. Vue.js——使用$.ajax和vue-resource实现OAuth的注册、登录、注销和API调用

    概述 上一篇我们介绍了如何使用vue resource处理HTTP请求,结合服务端的REST API,就能够很容易地构建一个增删查改应用.这个应用始终遗留了一个问题,Web App在访问REST AP ...

  7. Vue.js——vue-resource全攻略

    概述 上一篇我们介绍了如何将$.ajax和Vue.js结合在一起使用,并实现了一个简单的跨域CURD示例.Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的D ...

  8. vue.js插件使用(01) vue-resource

    本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...

  9. vue入门 vue与react和Angular的关系和区别

    一.为什么学习vue.js vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn.vuejs.org/ 手册:http://cn.vuejs.org/ ...

  10. 从零开始学习Vue.js,学习笔记

    一.为什么学习vue.js methods 只有纯粹的数据逻辑,而不是去处理 DOM 事件细节. vue.js兼具angular.js和react的优点,并且剔除了他们的缺点 官网:http://cn ...

随机推荐

  1. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

  2. python 杂七杂八 :

    subprocess.Popen(...) python3 实现代码: 备注write 要使用bytes 在最后结果转为str 显示时才不会出现 b'' 这样的字节显示 import subproce ...

  3. SpringCloud: 服务发现

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Helvetica Neue"; color: #e4af0a } sp ...

  4. 微信开发包注意jar版本:

    微信java jar的加密key的大小支持 异常java.security.InvalidKeyException:illegal Key Size的解决方案:在官方网站下载JCE无限制权限策略文件( ...

  5. mybatis-generator指定列进行自动生成代码

    目前mybatis-generator已经升级到1.3.3,功能比较强大,但是目前从table中如果字段较多可以选择忽略生产的字段(通过ignoreColumn属性实现,http://generato ...

  6. Greenplum记录(一):主体结构、master、segments节点、interconnect、performance monitor

    结构:Client--master host--interconnect--segment host 每个节点都是单独的PG数据库,要获得最佳的性能需要对每个节点进行独立优化. master上不包含任 ...

  7. SQLServer------将表和内容导入到另一个数据库

    转载: http://jingyan.baidu.com/article/d5c4b52bc5c102da570dc547.html

  8. 自己动手编写spring IOC源码

    前言:对于spring IOC概念不是很了解的朋友可以阅读我上一篇博客--轻松理解spring IOC(这两篇博客也是由于我的个人原因导致现在才发布,惭愧啊).通过这篇博客的理解之后,相信大家会对sp ...

  9. python 生成验证码

    在工作中经常遇到一些验证码,这些是怎么生成的呢,今天我用Python编写了下 import randomcode = []for i in range(6): if i == random.randi ...

  10. 使用JS,获取URL中指定参数的值

    /** * 获取URL中指定参数的值 * * @param name 参数名称 * @returns */ function getQueryString(name) { var reg = new ...