具体见链接
get传递参数
// 通过 params 设置参数:
axios.get('/user', {
params: {
ID: 12345
}
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
post传递参数
axios.post('/user', {
firstName: 'Fred', // 参数 firstName
lastName: 'Flintstone' // 参数 lastName
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
执行多个并发请求
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) {
// 两个请求现在都执行完成
}));
全局配置
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';
拦截器
请求拦截器
axios.interceptors.request.use(function (config) {
// 在发送请求之前做些什么
return config;
}, function (error) {
// 对请求错误做些什么
return Promise.reject(error);
});
响应拦截器
axios.interceptors.response.use(function (response) {
// 对响应数据做点什么
return response;
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error);
});
移除拦截器
var myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);
请求配置项
url: “/user” | url 是用于请求的服务器 URL |
method: “get”, | method 是创建请求时使用的方法 |
baseURL: “https://some-domain.com/api/”, | baseURL 将自动加在 url 前面, |
transformRequest: [function (data) { return data;}], | transformRequest 允许在向服务器发送前,修改请求数据 |
transformResponse: [function (data) {return data;}], | transformResponse 在传递给 then/catch 前,允许修改响应数据 |
headers: {“X-Requested-With”: “XMLHttpRequest”}, | headers 是即将被发送的自定义请求头 |
params: {ID: 12345}, | params 是即将与请求一起发送的 URL 参数 |
paramsSerializer: function(params) {return Qs.stringify(params, {arrayFormat: “brackets”})}, | paramsSerializer 是一个负责 params 序列化的函数 |
data: {firstName: “Fred”}, | data 是作为请求主体被发送的数据 |
timeout: 1000, | timeout 指定请求超时的毫秒数,超过将请求中断 |
withCredentials: false, | withCredentials 表示跨域请求时是否需要使用凭证 |
adapter: function (config) {} | adapter 允许自定义处理请求,以使测试更轻松 |
auth: {username: “janedoe”,password: “s00pers3cret”}, | auth 表示应该使用 HTTP 基础验证,并提供凭据 |
responseType: “json”, | responseType 表示服务器响应的数据类型, |
xsrfCookieName: “XSRF-TOKEN”, | xsrfCookieName 是用作 xsrf token 的值的cookie的名称 |
xsrfHeaderName: “X-XSRF-TOKEN”, | xsrfHeaderName 是承载 xsrf token 的值的 HTTP 头的名称 |
onUploadProgress: function (progressEvent) {}, | onUploadProgress 允许为上传处理进度事件 |
onDownloadProgress: function (progressEvent){} | onDownloadProgress 允许为下载处理进度事件 |
maxContentLength: 2000, | maxContentLength 定义允许的响应内容的最大尺寸 |
validateStatus: function (status) {return status >= 200 && status < 300; }, | validateStatus 定义对于给定的HTTP 响应状态码 |
maxRedirects: 5, // 默认的 | maxRedirects 定义在 node.js 中 follow 的最大重定向数目 |
httpAgent: new http.Agent({ keepAlive: true }) | httpAgent 和 httpsAgent 分别在 node.js 中用于定义在执行 http 和 https 时使用的自定义代理 |
proxy: {host: “”,port: ,auth: : { username: “mikeymike”,password: “rapunz3l”}}, | proxy 定义代理服务器的主机名称和端口auth 表示 HTTP 基础验证应当用于连接代理,并提供凭据 |
cancelToken: new CancelToken(function (cancel) {} | cancelToken 指定用于取消请求的 cancel token |
vue-resource
Vue 要实现异步加载需要使用到 vue-resource 库。
options 参数说明
url | 请求的目标URL |
body | 作为请求体发送的数据 |
headers | 作为请求头部发送的头部对象 |
params | 作为URL参数的参数对象 |
method | HTTP方法 (例如GET,POST,…) |
timeout | 请求超时(单位:毫秒) (0表示永不超时) |
before | 在请求发送之前修改请求的回调函数 |
progress | 用于处理上传进度的回调函数 ProgressEvent |
credentials | 是否需要出示用于跨站点请求的凭据 |
emulateHTTP | 是否需要通过设置X-HTTP-Method-Override头部并且以传统POST方式发送PUT,PATCH和DELETE请求。 |
emulateJSON | 设置请求体的类型为application/x-www-form-urlencoded |