axios 的用法

转载地址:https://blog.csdn.net/hsany330/article/details/71079745
参考地址:https://www.kancloud.cn/yunye/axios/234845

axios 简介

axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征:


  • 从浏览器中创建 XMLHttpRequest
  • 从 node.js 发出 http 请求
  • 支持 Promise API
  • 拦截请求和响应
  • 转换请求和响应数据
  • 取消请求
  • 自动转换JSON数据
  • 客户端支持防止 CSRF/XSRF


浏览器兼容性

引入方式:

$ npm install axios
$ cnpm install axios  //taobao源
$ bower install axios
或者使用cdn:
<script src= “https://unpkg.com/axios/dist/axios.min.js”></script>



举例说明:




执行 GET 请求

// 向具有指定ID的用户发出请求
axios.get( ‘/user?ID=12345’)
.then( function (response) {
console.log(response);
})
.catch( function (error) {
console.log(error);
});
 
// 也可以通过 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’,
lastName: ’Flintstone’
})
.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 API




可以通过将相关配置传递给 axios 来进行请求。

axios(config)

// 发送一个 POST 请求
axios({
method: ’post’,
url: ’/user/12345’,
data: {
firstName: ’Fred’,
lastName: ’Flintstone’
}
});



axios(url[, config])


1
2


// 发送一个 GET 请求 (GET请求是默认请求模式)
axios( ‘/user/12345’);



请求方法别名

为了方便起见,已经为所有支持的请求方法提供了别名。

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


注意

当使用别名方法时,不需要在config中指定url,method和data属性。

并发

帮助函数处理并发请求。

  • axios.all(iterable)
  • axios.spread(callback)


创建实例

您可以使用自定义配置创建axios的新实例。

axios.create([config])

1
2
3
4
5


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是必需的。 如果未指定方法,请求将默认为GET。




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131

{
// `url`是将用于请求的服务器URL
url:  ‘/user’,
 
// `method`是发出请求时使用的请求方法
method:  ‘get’, // 默认
 
// `baseURL`将被添加到`url`前面,除非`url`是绝对的。
// 可以方便地为 axios 的实例设置`baseURL`,以便将相对 URL 传递给该实例的方法。
baseURL:  ‘https://some-domain.com/api/’,
 
// `transformRequest`允许在请求数据发送到服务器之前对其进行更改
// 这只适用于请求方法’PUT’,’POST’和’PATCH’
// 数组中的最后一个函数必须返回一个字符串,一个 ArrayBuffer或一个 Stream
 
transformRequest: [ function (data) {
// 做任何你想要的数据转换
 
return data;
}],
 
// `transformResponse`允许在 then / catch之前对响应数据进行更改
transformResponse: [ function (data) {
// Do whatever you want to transform the data
 
return data;
}],
 
// `headers`是要发送的自定义 headers
headers: { ‘X-Requested-With’: ’XMLHttpRequest’},
 
// `params`是要与请求一起发送的URL参数
// 必须是纯对象或URLSearchParams对象
params: {
ID: 12345
},
 
// `paramsSerializer`是一个可选的函数,负责序列化`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`是要作为请求主体发送的数据
// 仅适用于请求方法“PUT”,“POST”和“PATCH”
// 当没有设置`transformRequest`时,必须是以下类型之一:
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream
data: {
firstName: ’Fred’
},
 
// `timeout`指定请求超时之前的毫秒数。
// 如果请求的时间超过’timeout’,请求将被中止。
timeout:  1000,
 
// `withCredentials`指示是否跨站点访问控制请求
// should be made using credentials
withCredentials:  false, // default
 
// `adapter’允许自定义处理请求,这使得测试更容易。
// 返回一个promise并提供一个有效的响应(参见[response docs](#response-api))
adapter:  function (config) {
/* … */
},
 
// `auth’表示应该使用 HTTP 基本认证,并提供凭据。
// 这将设置一个`Authorization’头,覆盖任何现有的`Authorization’自定义头,使用`headers`设置。
auth: {
username: ’janedoe’,
password: ’s00pers3cret’
},
 
// “responseType”表示服务器将响应的数据类型
// 包括 ‘arraybuffer’, ‘blob’, ‘document’, ‘json’, ‘text’, ‘stream’
responseType:  ‘json’, // default
 
//`xsrfCookieName`是要用作 xsrf 令牌的值的cookie的名称
xsrfCookieName:  ‘XSRF-TOKEN’, // default
 
// `xsrfHeaderName`是携带xsrf令牌值的http头的名称
xsrfHeaderName:  ‘X-XSRF-TOKEN’, // default
 
// `onUploadProgress`允许处理上传的进度事件
onUploadProgress:  function (progressEvent) {
// 使用本地 progress 事件做任何你想要做的
},
 
// `onDownloadProgress`允许处理下载的进度事件
onDownloadProgress:  function (progressEvent) {
// Do whatever you want with the native progress event
},
 
// `maxContentLength`定义允许的http响应内容的最大大小
maxContentLength:  2000,
 
// `validateStatus`定义是否解析或拒绝给定的promise
// HTTP响应状态码。如果`validateStatus`返回`true`(或被设置为`null` promise将被解析;否则,promise将被
   // 拒绝。
validateStatus:  function (status) {
return status >= 200 && status < 300; // default
},
 
// `maxRedirects`定义在node.js中要遵循的重定向的最大数量。
// 如果设置为0,则不会遵循重定向。
maxRedirects:  5, // 默认
 
// `httpAgent`和`httpsAgent`用于定义在node.js中分别执行http和https请求时使用的自定义代理。
// 允许配置类似`keepAlive`的选项,
// 默认情况下不启用。
httpAgent:  new http.Agent({ keepAlive: true }),
httpsAgent: new https.Agent({ keepAlive: true }),
 
// ‘proxy’定义代理服务器的主机名和端口
// `auth`表示HTTP Basic auth应该用于连接到代理,并提供credentials。
// 这将设置一个`Proxy-Authorization` header,覆盖任何使用`headers`设置的现有的`Proxy-Authorization` 自定义 headers。
proxy: {
host: ’127.0.0.1’,
port: 9000,
auth: : {
username: ’mikeymike’,
password: ’rapunz3l’
}
},
 
// “cancelToken”指定可用于取消请求的取消令牌
// (see Cancellation section below for details)
cancelToken:  new CancelToken(function (cancel) {
})
}


使用 then 时,您将收到如下响应:




1
2
3
4
5
6
7
8

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默认值

1
2
3


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’;



自定义实例默认值


1
2
3
4
5
6
7


//在创建实例时设置配置默认值
var instance = axios.create({
   baseURL: ‘https://api.example.com’
});
 
//在实例创建后改变默认值
instance.defaults.headers.common [ ‘Authorization’] = AUTH_TOKEN;



配置优先级顺序

配置将与优先顺序合并。 顺序是lib / defaults.js中的库默认值,然后是实例的defaults属性,最后是请求的config参数。 后者将优先于前者。 这里有一个例子。

1
2
3
4
5
6
7
8
9
10
11
12


//使用库提供的配置默认值创建实例
//此时,超时配置值为`0`,这是库的默认值
var instance = axios.create();
 
//覆盖库的超时默认值
//现在所有请求将在超时前等待2.5秒
instance.defaults.timeout =  2500;
 
//覆盖此请求的超时,因为它知道需要很长时间
instance.get( ‘/ longRequest’,{
   timeout: 5000
});



拦截器




你可以截取请求或响应在被 then 或者 catch 处理之前




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

//添加请求拦截器
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);
   });


如果你以后可能需要删除拦截器。




1
2

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


你可以将拦截器添加到axios的自定义实例。




1
2

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


处理错误





1
2
3
4
5
6
7
8
9
10
11
12
13
14

axios.get( ‘/ user / 12345’)
   .catch( function(error){
      if(error.response){
        //请求已发出,但服务器使用状态代码进行响应
        //落在2xx的范围之外
        console.log(error.response.data);
        console.log(error.response.status);
        console.log(error.response.headers);
     }  else {
        //在设置触发错误的请求时发生了错误
        console.log(’Error’,error.message);
     }}
      console.log(error.config);
   });


您可以使用validateStatus配置选项定义自定义HTTP状态码错误范围。




1
2
3
4
5

axios.get( ‘/ user / 12345’,{
   validateStatus: function(status){
      return status < 500; //仅当状态代码大于或等于500时拒绝
   }}
})


消除




您可以使用取消令牌取消请求。

axios cancel token API基于可取消的promise提议,目前处于阶段1。

您可以使用CancelToken.source工厂创建一个取消令牌,如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


var CancelToken = axios.CancelToken;
var source = CancelToken.source();
 
axios.get( ‘/user/12345’, {
cancelToken: source.token
}).catch( function(thrown) {
if (axios.isCancel(thrown)) {
console.log(’Request canceled’, thrown.message);
else {
// 处理错误
}
});
 
//取消请求(消息参数是可选的)
source.cancel( ‘操作被用户取消。’);



您还可以通过将执行器函数传递给CancelToken构造函数来创建取消令牌:




1
2
3
4
5
6
7
8
9
10
11
12

var CancelToken = axios.CancelToken;
var cancel;
 
axios.get( ‘/ user / 12345’,{
   cancelToken: new CancelToken(function executor(c){
      //一个执行器函数接收一个取消函数作为参数
     cancel = c;
   })
});
 
// 取消请求
clear();

注意:您可以使用相同的取消令牌取消几个请求。

使用application / x-www-form-urlencoded格式



默认情况下,axios将JavaScript对象序列化为JSON。 要以应用程序/ x-www-form-urlencoded格式发送数据,您可以使用以下选项之一。

浏览器

在浏览器中,您可以使用URLSearchParams API,如下所示:

1
2
3
4


var params = new URLSearchParams();
params.append( ‘param1’, ’value1’);
params.append( ‘param2’, ’value2’);
axios.post( ‘/foo’, params);


请注意,所有浏览器都不支持URLSearchParams,但是有一个polyfill可用(确保polyfill全局环境)。

或者,您可以使用qs库对数据进行编码:

1
2


var qs = require(’qs’);
axios.post( ‘/foo’, qs.stringify({ ’bar’: 123 });



Node.js

在node.js中,可以使用querystring模块,如下所示:

1
2


var querystring = require(’querystring’);
axios.post( ‘http://something.com/’, querystring.stringify({ foo: ’bar’ });



你也可以使用qs库。

Promise



axios 依赖本机要支持ES6 Promise实现。 如果您的环境不支持ES6 Promises,您可以使用polyfill。

TypeScript



axios包括TypeScript定义。




1
2

import axios from ’axios’;
axios.get( ‘/user?ID=12345’);



axios在很大程度上受到Angular提供的 httpaxiosAngular使 h t t p 服 务 的 启 发 。 最 终 , a x i o s 努 力 提 供 一 个 在 A n g u l a r 外 使 用 的 独 立 的 http-like服务。

转载:http://www.cnblogs.com/libin-1/p/6445356.html

            </div>
                </div>
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中使用axios进行数据请求的用法如下: 1. 首先,在项目的main.js文件中导入axios并将其挂载到Vue的原型上,以便在整个项目中都可以使用axios进行请求。例如:import axios from 'axios' Vue.prototype.$axios = axios。 2. 在需要发送请求的组件中,可以通过this.$axios来调用axios的各种方法进行数据请求。 - 使用get方法发送get请求,可以通过传入请求的url和可选的参数进行请求。例如:this.$axios.get(url, params)。 - 使用post方法发送post请求,可以通过传入请求的url和需要发送的数据进行请求。例如:this.$axios.post(url, data)。 - 使用put方法发送put请求,可以通过传入请求的url和需要修改的数据进行请求。例如:this.$axios.put(url, data)。 - 使用delete方法发送delete请求,可以通过传入请求的url和可选的参数进行请求。例如:this.$axios.delete(url, params)。 3. 可以通过axios的拦截器来对请求或响应进行处理。 - 使用axios的请求拦截器,可以在请求发送前对请求的配置进行修改。例如:axios.interceptors.request.use(config => return config)。 - 使用axios的响应拦截器,可以对服务器返回的数据进行处理并返回处理后的数据。例如:axios.interceptors.response.use(res => return res.data)。 4. 创建axios实例可以通过axios.create({})来创建,可以传入一些参数进行配置,如baseUrl、method和timeout等。例如:axios.create({})。 5. 可以通过axios.defaults来设置全局的默认配置,例如设置baseUrl、method和timeout等。例如:axios.defaults.xxx。 6. 使用axios取消请求,可以通过创建取消请求的错误对象和取消请求的token对象来实现。例如:axios.CancelToken()和axios.isCancel()。 7. 使用axios.all可以批量执行多个异步任务,传入多个promise对象进行执行。例如:axios.all(promise)。 综上所述,以上是Vue中使用axios进行数据请求的一些常用用法。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [vue-axios使用](https://blog.csdn.net/m0_57391092/article/details/126405908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [vue axios用法教程详解](https://download.csdn.net/download/weixin_38513669/12776235)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值