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])

选项

参数 类型 描述
url string 请求发送到的 URL
body Object, FormData, string 请求中要发送的数据
headers Object 作为 HTTP 请求头发送的 Headers 对象
params Object 作为 URL 参数发送的 Parameters 对象
method string HTTP 方法 (例如: GET, POST, ...)
timeout number 请求超时的毫秒数 (0 为不超时)
before function(request) 在请求发送之前用于修改请求选项的回调函数
progress function(event) 上传时用于控制 ProgressEvent 的回调函数
credentials boolean Indicates whether or not cross-site Access-Control requests should be made using credentials
emulateHTTP boolean 使用 HTTP POST 发送 PUT, PATCH 和 DELETE 请求并设置 X-HTTP-Method-Override
emulateJSON boolean application/x-www-form-urlencoded 内容类型发送请求报文

响应

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

属性 类型 描述
url string 响应的源 URL
body Object, Blob, string 响应的数据报文
headers Header 响应头对象
ok boolean 从 200 到 299 的 HTTP 状态码
status number 响应中的 HTTP 状态码
statusText string 响应中的 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'
  }));
});
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,856评论 19 139
  • 由于公司前端人员短缺,这周我和涛哥就来填这个坑了,前端使用的js框架是Vue+Vue-resource+Vue-v...
    郭之源阅读 50,413评论 9 57
  • 一、YYLabel 强大的富文本显示功能,可根据文字的range随意添加点击事件 1 自动换行 YYLabel *...
    大白菜s阅读 14,393评论 2 21

友情链接更多精彩内容