Vue学习之路之vue-resource

安装

  • <script src="https://cdn.jsdelivr.net/npm/[email protected]"></script> (引入脚本直接使用)
  • npm install vue-resource --save(使用npm 安装)

请求API(常用)

  • get(url.[options])
  • head(url.[options])
  • delete(url.[options])
  • jsonp(url.[options])
  • post(url.[options])
  • put(url.[options])
  • patch(url.[options])

常用参数

参数 类型 描述
url string 请求的rul
method string 请求HTTP方法,例如:‘GET’,'POST’或其他HTTP方法
body Object,FormDatastring request body
parms Object 请求URL参数对象
headers Object request header
timeout num 单位未毫秒的请求超时时间(0表示无超时间)
before function(request) 请求发送前的处理函数,类似与jQuery的beforeSend函数
progress function(event) ProgressEvent回调处理函数
credientials boolean 请求跨域请求时是否需要使用凭证
emulateHTTP boolean 发送PUT,PATCH,DELETE请求时以HTTP POST的方式发送,并设置请求头的X-HTTP-Method_Override
emulateJSON boolean 将request body以application/x-www-form-urlencoded content type发送

全局拦截器

 
Vue.http.interceptors.push((request, next) => {
 console.log(this)//此处this为请求所在页面的Vue实例
  // modify request
  request.method = 'POST';//在请求之前可以进行一些预处理和配置

  // continue to next interceptor

  next((response) => {//在响应之后传给then之前对response进行修改和逻辑判断。对于token时候已过期的判断,就添加在此处,页面中任何一次http请求都会先调用此处方法

      response.body = '...';
    return response;

  });
});```


猜你喜欢

转载自blog.csdn.net/u012967454/article/details/84280906
今日推荐