vue-resource HTTP

HTTP

可以使用全局的 Vue.http 或者在 Vue 实例中的 this.$http 调用 http 服务。

使用

Vue 实例提供了 this.$http 服务可用于发送 HTTP 请求。 A request method call returns a Promise that resolves to the response. Vue 实例在所有回调方法中都会自动绑定到 this 里。

{
  // GET /someUrl
  this.$http.get('/someUrl').then((response) => {
    // success callback
  }, (response) => {
    // error callback
  });
}

方法

所有的请求类型都可以使用短函数,可以在全局或者 Vue 实例中使用。

// 全局 Vue 对象
Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);

// Vue 实例
this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
this.$http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);

短方法清单:

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

选项

参数类型描述
urlstring请求发送到的 URL
bodyObject, FormData, string请求中要发送的数据
headersObject作为 HTTP 请求头发送的 Headers 对象
paramsObject作为 URL 参数发送的 Parameters 对象
methodstringHTTP 方法 (例如: GET, POST, ...)
timeoutnumber请求超时的毫秒数 (0 为不超时)
beforefunction(request)在请求发送之前用于修改请求选项的回调函数
progressfunction(event)上传时用于控制 ProgressEvent 的回调函数
credentialsbooleanIndicates whether or not cross-site Access-Control requests should be made using credentials
emulateHTTPboolean使用 HTTP POST 发送 PUT, PATCH 和 DELETE 请求并设置 X-HTTP-Method-Override
emulateJSONbooleanapplication/x-www-form-urlencoded 内容类型发送请求报文

响应

通过下面的属性和函数将请求解析为响应对象:

属性类型描述
urlstring响应的源 URL
bodyObject, Blob, string响应的数据报文
headersHeader响应头对象
okboolean从 200 到 299 的 HTTP 状态码
statusnumber响应中的 HTTP 状态码
statusTextstring响应中的 HTTP 状态文本
函数类型描述
text()Promise作为字符串解析报文
json()Promise作为 Json 对象解析报文
blob()Promise作为 Blob 对象解析报文

实例

{
  // POST /someUrl
  this.$http.post('/someUrl', {foo: 'bar'}).then((response) => {

    // get status
    response.status;

    // get status text
    response.statusText;

    // get 'Expires' header
    response.headers.get('Expires');

    // set data on vm
    this.$set('someData', response.body);

  }, (response) => {
    // error callback
  });
}

获取图像并使用 blob() 方法来从响应中提取图像正文内容。

{
  // GET /image.jpg
  this.$http.get('/image.jpg').then((response) => {

    // resolve to Blob
    return response.blob();

  }).then(blob) => {
    // use image Blob
  });
}

拦截器

可以全局定义一个拦截器,用于预处理和后处理的请求。

请求处理

Vue.http.interceptors.push((request, next) => {

  // modify request
  request.method = 'POST';

  // continue to next interceptor
  next();
});

请求和响应处理

Vue.http.interceptors.push((request, next)  => {

  // modify request
  request.method = 'POST';

  // continue to next interceptor
  next((response) => {

    // modify response
    response.body = '...';

  });
});

返回一个响应并停止处理

Vue.http.interceptors.push((request, next) => {

  // modify request ...

  // stop and return response
  next(request.respondWith(body, {
    status: 404,
    statusText: 'Not found'
  }));
});

 

 



作者:伍源辉
链接:https://www.jianshu.com/p/ed9e98731d96
來源:简书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值