Vue中发送ajax请求——axios使用详解

摘要:
并发处理并发请求的帮助方法axios.allaxios.spread创建一个实例你可以用自定义配置创建一个新的axios实例。axios.createvarinstance=axios.create;实例方法所有可用的实例方法都列在下面了,指定的配置将会和该实例的配置合并。axios#requestaxios#getaxios#deleteaxios#headaxios#postaxios#putaxios#patch请求配置下面是可用的请求配置项,只有url是必需的。如果没有指定method,默认的请求方法是GET。

Vue中发送ajax请求——axios使用详解
目录
axios

基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用
功能特性

在浏览器中发送 XMLHttpRequests 请求
在 node.js 中发送 http请求
支持 Promise API
拦截请求和响应
转换请求和响应数据
自动转换 JSON 数据
客户端支持保护安全免受 XSRF 攻击

回到顶部
浏览器支持

Browser Matrix
回到顶部
安装

使用 bower:

$ bower install axios

使用 npm:

$ npm install axios

回到顶部
例子

发送一个 GET 请求
复制代码

// Make a request for a user with a given ID
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});
// Optionally the request above could also be done as
axios.get('/user', {params: {ID: 12345}})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});

复制代码

发送一个 POST 请求
复制代码

axios.post('/user', {firstName: 'Fred', lastName: 'Flintstone'})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});

复制代码

发送多个并发请求
复制代码

function getUserAccount() {
return axios.get('/user/12345');
}

function getUserPermissions() {
return axios.get('/user/12345/permissions');
}

axios.all([getUserAccount(), getUserPermissions()])
.then(axios.spread(function (acct, perms) {
// Both requests are now complete
}));

复制代码

axios API

可以通过给 axios传递对应的参数来定制请求:
axios(config)
复制代码

// Send a POST request
axios(
{
method: 'post',
url: '/user/12345',
data: {
firstName: 'Fred',
lastName: 'Flintstone'
}
});

复制代码
axios(url[, config])

// Sned a GET request (default method)
axios('/user/12345');

请求方法别名

为方便起见,我们为所有支持的请求方法都提供了别名
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
注意

当使用别名方法时, url、 method 和 data 属性不需要在 config 参数里面指定。
并发

处理并发请求的帮助方法
axios.all(iterable)
axios.spread(callback)
创建一个实例

你可以用自定义配置创建一个新的 axios 实例。
axios.create([config])

var instance = axios.create({
baseURL: 'https://some-domain.com/api/',
timeout: 1000,
headers: {'X-Custom-Header': 'foobar'}
});

实例方法

所有可用的实例方法都列在下面了,指定的配置将会和该实例的配置合并。
axios#request(config)
axios#get(url[, config])
axios#delete(url[, config])
axios#head(url[, config])
axios#post(url[, data[, config]])
axios#put(url[, data[, config]])
axios#patch(url[, data[, config]])

请求配置

下面是可用的请求配置项,只有 url 是必需的。如果没有指定 method ,默认的请求方法是 GET。

{
// url is the server URL that will be used for the request
url:'/user',
// method is the request method to be used when making the request
method: 'get', // default
// baseURL will be prepended to url unless url is absolute.
// It can be convenient to set baseURL for an instance of axios to pass relative URLs
// to methods of that instance.
baseURL: 'https://some-domain.com/api/',
// transformRequest allows changes to the request data before it is sent to the server
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
// The last function in the array must return a string or an ArrayBuffer
transformRequest: [function (data) {
// Do whatever you want to transform the data return data;
}],
// transformResponse allows changes to the response data to be made before
// it is passed to then/catch
transformResponse: [function (data) {
// Do whatever you want to transform the data return data;
}],
// headers are custom headers to be sent
headers: {'X-Requested-With': 'XMLHttpRequest'},
// params are the URL parameters to be sent with the request
params: { ID: 12345 };
// paramsSerializer is an optional function in charge of serializing params
// (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
paramsSerializer: function(params) {
return Qs.stringify(params, {arrayFormat: 'brackets'})
},
// data is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no transformRequest is set, must be a string, an ArrayBuffer or a hash
data: { firstName: 'Fred' },
// timeout specifies the number of milliseconds before the request times out.
// If the request takes longer than timeout, the request will be aborted.
timeout: 1000,
// withCredentials indicates whether or not cross-site Access-Control requests
// should be made using credentials
withCredentials: false, // default
// adapter allows custom handling of requests which makes testing easier.
// Call resolve or reject and supply a valid response (see response docs).
adapter: function (resolve, reject, config) { /* ... */ },
// auth indicates that HTTP Basic auth should be used, and supplies credentials.
// This will set an Authorization header, overwriting any existing
// Authorization custom headers you have set using headers.
auth: { username: 'janedoe', password: 's00pers3cret' }
// responseType indicates the type of data that the server will respond with
// options are 'arraybuffer', 'blob', 'document', 'json', 'text'
responseType: 'json', // default
// xsrfCookieName is the name of the cookie to use as a value for xsrf token
xsrfCookieName: 'XSRF-TOKEN', // default
// xsrfHeaderName is the name of the http header that carries the xsrf token value
xsrfHeaderName: 'X-XSRF-TOKEN', // default
// progress allows handling of progress events for 'POST' and 'PUT uploads' as well as 'GET' downloads
progress: function(progressEvent) {
// Do whatever you want with the native progress event
}
}

响应的数据结构

响应的数据包括下面的信息:

{
// data is the response that was provided by the server
data: {},
// status is the HTTP status code from the server response
status: 200,
// statusText is the HTTP status message from the server response
statusText: 'OK',
// headers the headers that the server responded with
headers: {},
// config is the config that was provided to axios for the request
config: {}
}

当使用 then 或者 catch 时, 你会收到下面的响应:

axios.get('/user/12345').then(function (response) {
console.log(response.data);
console.log(response.status);
console.log(response.statusText);
console.log(response.headers);
console.log(response.config);
});

你可以为每一个请求指定默认配置。
全局 axios 默认配置

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

自定义实例默认配置

// Set config defaults when creating the instance
var instance = axios.create({baseURL: 'https://api.example.com'});
// Alter defaults after instance has been created
instance.defaults.headers.common['Authorization'] = AUTH_TOKEN;

配置的优先顺序

Config will be merged with an order of precedence. The order is library defaults found in lib/defaults.js, then defaults property of the instance, and finally config argument for the request. The latter will take precedence over the former. Here's an example.

// Create an instance using the config defaults provided by the library
// At this point the timeout config value is 0 as is the default for the library
var instance = axios.create();
// Override timeout default for the library
// Now all requests will wait 2.5 seconds before timing out
instance.defaults.timeout = 2500;
// Override timeout for this request as it's known to take a long time
instance.get('/longRequest', {timeout: 5000});

你可以在处理 then 或 catch 之前拦截请求和响应

// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
// Do something before request is sent return config;
}, function (error) {
// Do something with request error return Promise.reject(error);
});
// 添加一个响应拦截器
axios.interceptors.response.use(function (response) {
// Do something with response data return response;
}, function (error) {
// Do something with response error return Promise.reject(error);
});

移除一个拦截器:

var myInterceptor = axios.interceptors.request.use(function () {
/.../
});
axios.interceptors.request.eject(myInterceptor);

你可以给一个自定义的 axios 实例添加拦截器:

var instance = axios.create();
instance.interceptors.request.use(function () {
/.../
});

axios.get('/user/12345').catch(function (response) {
if (response instanceof Error) {
// Something happened in setting up the request that triggered an Error
console.log('Error', response.message);
} else {
// The request was made, but the server responded with a status code
// that falls out of the range of 2xx
console.log(response.data);
console.log(response.status);
console.log(response.headers);
console.log(response.config);
}
});

Promises

axios 依赖一个原生的 ES6 Promise 实现,如果你的浏览器环境不支持 ES6 Promises,你需要引入 polyfill
TypeScript

axios 包含一个 TypeScript 定义

///
import * as axios from 'axios';
axios.get('/user?ID=12345');

Credits

axios is heavily inspired by the $http service provided in Angular. Ultimately axios is an effort to provide a standalone $http-like service for use outside of Angular.
License

免责声明:文章转载自《Vue中发送ajax请求——axios使用详解》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇潭州课堂25班:Ph201805201 爬虫基础 第六课 选择器 bs4 (课堂笔记)Redis——集群(cluster)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[vue/no-parsing-error] Parsing error: invalid-first-character-of-tag-name.eslint-plugin-vue Parsing error: invalid-first-character-of-tag-name.eslint(vue/no-parsing-error)

问题描述: vue页面中使用插值语法和三元表达式,出现以上报错,但是可以正常运行; 报错代码如: <p>{{num<0?"你好":"hello"}}</> 报错信息 [vue/no-parsing-error] Parsing error: invalid-first-character-of-tag-name.eslin...

使用 Vue 开发 scrollbar 滚动条组件

Vue 应该说是很火的一款前端库了,和 React 一样的高热度,今天就来用它写一个轻量的滚动条组件; 知识储备:要开发滚动条组件,需要知道知识点是如何计算滚动条的大小和位置,还有一个问题是如何监听容器大小的改变,然后更新滚动条的位置; 先把样式贴出来: .disable-selection { -webkit-touch-callout: none...

网络_01 基本配置

一.交换机工作模式的进入与退出 Switch>enable进入特权模式 Switch#configure terminal 进入全局配置模式 Switch(config)#interface fastEthernet 0/1 进入接口模式 退出:exit(e) 二.基本配置信息 Switch(config)# hostname s1 配置主机名 Sw...

WinForm 创建与读写配置文件

1. 创建 app.config 文件:右击项目名称,选择“添加”→“添加新建项”,在出现的“添加新项”对话框中,选择“添加应用程序配置文件”;如果项目以前没有配置文件,则默认的文件名称为“app.config”,单击“确定”。 出现在设计器视图中的app.config文件为: <?xml version="1.0" encoding="utf...

WebSocket原理及与http1.0/1.1 long poll跟 ajax轮询的区别【转自知乎】

今天学习了几个以前没有见过的东东,作者的文章写的还是很通熟易懂的!! 起码我基本都看懂了(2333)————正文 今天要学习的是WebSocket原理与http1.0/1.1 long poll 和 ajax轮询的区别 WebSocket是HTML5出的东西,也就是说HTTP谢意没有变化,或者说没有关系。首先HTTP有1.1和1.0直说,也就是所谓的kee...

Nacos Config 多环境的配置

Spring Boot Profile 我们在做项目开发的时候,生产环境和测试环境的一些配置可能会不一样,有时候一些功能也可能会不一样,所以我们可能会在上线的时候手工修改这些配置信息。但是 Spring 中为我们提供了 Profile 这个功能。我们只需要在启动的时候添加一个虚拟机参数,激活自己环境所要用的 Profile 就可以了。 操作起来很简单,只需...