HTTP

你可以使用全局对象方式Vue.http或者在一个Vue实例的内部使用this.$http来发起HTTP请求。

语法

一个Vue实例内部提供的this.$http服务可以发送HTTP请求。一个请求方法的回调请参考 Promise它可以解决这些响应。当然Vue实例将自动绑定到this在所有的函数回调中。

  1. {
  2. // GET /someUrl
  3. this.$http.get('/someUrl').then((response) => {
  4. // success callback
  5. }, (response) => {
  6. // error callback
  7. });
  8. }

方法

我们为所有的请求类型提供了一套简便的方法。 这些方法可以在全局或一个Vue实例中工作。

  1. // global Vue object
  2. Vue.http.get('/someUrl', [options]).then(successCallback, errorCallback);
  3. Vue.http.post('/someUrl', [body], [options]).then(successCallback, errorCallback);
  4. // in a Vue instance
  5. this.$http.get('/someUrl', [options]).then(successCallback, errorCallback);
  6. 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 作为请求头部发送的头部对象
params Object 作为URL参数的参数对象
method string HTTP方法 (例如GET,POST,…)
timeout number 请求超时(单位:毫秒) (0表示永不超时)
before function(request) 在请求发送之前修改请求的回调函数
progress function(event) 用于处理上传进度的回调函数 ProgressEvent
credentials boolean 是否需要出示用于跨站点请求的凭据
emulateHTTP boolean 是否需要通过设置X-HTTP-Method-Override头部并且以传统POST方式发送PUT,PATCH和DELETE请求。
emulateJSON boolean 设置请求体的类型为application/x-www-form-urlencoded

响应

通过如下属性和方法处理一个请求获取到的响应对象:

属性 类型 描述
url string 响应的URL源
body Object, Blob, string 响应体数据
headers Header 请求头部对象
ok boolean 当HTTP响应码为200到299之间的数值时该值为true
status number HTTP响应吗
statusText string HTTP响应状态
方法 类型 描述
text() 约定值 以字符串方式返回响应体
json() 约定值 以格式化后的json对象方式返回响应体
blob() 约定值 以二进制Blob对象方式返回响应体

例子

  1. {
  2. // POST /someUrl
  3. this.$http.post('/someUrl', {foo: 'bar'}).then((response) => {
  4. // get status
  5. response.status;
  6. // get status text
  7. response.statusText;
  8. // get 'Expires' header
  9. response.headers.get('Expires');
  10. // set data on vm
  11. this.$set('someData', response.body);
  12. }, (response) => {
  13. // error callback
  14. });
  15. }

使用blob()方法从响应中获取一副图像的内容。

  1. {
  2. // GET /image.jpg
  3. this.$http.get('/image.jpg').then((response) => {
  4. // resolve to Blob
  5. return response.blob();
  6. }).then(blob) => {
  7. // use image Blob
  8. });
  9. }

拦截器

全局定义拦截器后,它可用于前置和后置处理请求。

请求处理

  1. Vue.http.interceptors.push((request, next) => {
  2. // modify request
  3. request.method = 'POST';
  4. // continue to next interceptor
  5. next();
  6. });

请求与响应处理

  1. Vue.http.interceptors.push((request, next) => {
  2. // modify request
  3. request.method = 'POST';
  4. // continue to next interceptor
  5. next((response) => {
  6. // modify response
  7. response.body = '...';
  8. });
  9. });

返回一个响应并且停止处理

  1. Vue.http.interceptors.push((request, next) => {
  2. // modify request ...
  3. // stop and return response
  4. next(request.respondWith(body, {
  5. status: 404,
  6. statusText: 'Not found'
  7. }));
  8. });