代替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. java第二周周学习总结

    java运算符和循环 java运算符 一.for 语句 for 语句的基本结构如下所示:for(初始化表达式;判断表达式;递增(递减)表达式){    执行语句;   //一段代码} 初始化表达式:初 ...

  2. ElasticSearch中bulkProcesser使用

    初次接触es,可能对增删改查很熟悉,以为能为得心应手,本次应用场景为 数据库变更一条记录,会触发更新es中的数据,每秒并发大概30条左右,测试环境一切工作正常(数据量较少),上线后发现日志中很多类似于 ...

  3. 【JavaWeb】Spring+SpringMVC+MyBatis+SpringSecurity+EhCache+JCaptcha 完整Web基础框架(三)

    Spring+SpringMVC MVC呢,现在似乎越来越流行使用SpringMVC框架,我自己用的感觉,是非常好,确实很舒服,配置一开始是麻烦了一点点,但是后续的开发真的是很清爽! SpringMV ...

  4. 5 Django系列之通过list_display展示多对多与外键内容在admin-web界面下

    list_display展示多对多关系的内容 表结构关系 表一 class Server(models.Model): asset = models.OneToOneField('Asset') cr ...

  5. thinkphp访问不存在的模块或者方法跳转到404页面

    使用的thinkphp 版本是3.2.0, 在config.php中配置 404地址,即可: 'TMPL_EXCEPTION_FILE' => './Application/Home/View/ ...

  6. phpcms首页实现轮播图

    1.在你想要加轮播图的位置加入以下 <div id="flowDiagram" > <div id="button"> <span ...

  7. 借助Html制作渐变的网页背景颜色

    借助Html制作渐变的网页背景颜色 <html> <head> <title>制作渐变背景</title> <meta http-equiv=&q ...

  8. Java之使用Hadoop探索大数据的世界

    什么是大数据 PB = 1024tb 7123913827189tb Reids 无共享 HDFS 优点 :特别适合存储大型文件 TFS hdfs 架构 NameNode: 整个hadoop总管,只有 ...

  9. web页面之响应式布局

    一.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端——而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网 ...

  10. 基于linux(centos)的svn环境搭建

    1. 安装svn yum intall subversion 2. 查看安装位置 rpm -ql subversion 3. 检验svn是否安装成功,查看帮助 svn --help , 看到下图表示成 ...