我使用Fetch调用web服务,但我可以在Axios的帮助下做同样的事情。现在我很困惑。我应该选择Axios还是Fetch?
当前回答
Fetch和Axios在功能上非常相似,但为了更多的向后兼容性,Axios似乎工作得更好(例如,Fetch在IE 11中不能工作,请查看这篇文章)。
此外,如果您使用JSON请求,以下是我偶然发现的一些差异。
获取JSON post请求
let url = 'https://someurl.com';
let options = {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify({
property_one: value_one,
property_two: value_two
})
};
let response = await fetch(url, options);
let responseOK = response && response.ok;
if (responseOK) {
let data = await response.json();
// do something with data
}
Axios JSON post请求
let url = 'https://someurl.com';
let options = {
method: 'POST',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
data: {
property_one: value_one,
property_two: value_two
}
};
let response = await axios(options);
let responseOK = response && response.status === 200 && response.statusText === 'OK';
if (responseOK) {
let data = await response.data;
// do something with data
}
So:
Fetch's body = Axios' data Fetch's body has to be stringified, Axios' data contains the object Fetch has no url in request object, Axios has url in request object Fetch request function includes the url as parameter, Axios request function does not include the url as parameter. Fetch request is ok when response object contains the ok property, Axios request is ok when status is 200 and statusText is 'OK' To get the json object response: in fetch call the json() function on the response object, in Axios get data property of the response object.
其他回答
Axios是一个基于承诺的HTTP客户端库,而Fetch是一个用于发出API请求的javascript API。
主要区别是浏览器支持:Axios支持包括IE在内的所有浏览器,而Fetch只支持最新的浏览器,IE不支持。
参考链接:https://github.com/axios/axios浏览器支持
https://caniuse.com/fetch
与获取API相比,Axios具有更好的错误处理。Axios可以抛出400到500个范围的状态代码错误,而在fetch API中,您需要手动处理这些错误。 更多信息请访问:https://bariablogger.in/f/axios-vs-fetch-react
Fetch和Axios在功能上非常相似,但为了更多的向后兼容性,Axios似乎工作得更好(例如,Fetch在IE 11中不能工作,请查看这篇文章)。
此外,如果您使用JSON请求,以下是我偶然发现的一些差异。
获取JSON post请求
let url = 'https://someurl.com';
let options = {
method: 'POST',
mode: 'cors',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
body: JSON.stringify({
property_one: value_one,
property_two: value_two
})
};
let response = await fetch(url, options);
let responseOK = response && response.ok;
if (responseOK) {
let data = await response.json();
// do something with data
}
Axios JSON post请求
let url = 'https://someurl.com';
let options = {
method: 'POST',
url: url,
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json;charset=UTF-8'
},
data: {
property_one: value_one,
property_two: value_two
}
};
let response = await axios(options);
let responseOK = response && response.status === 200 && response.statusText === 'OK';
if (responseOK) {
let data = await response.data;
// do something with data
}
So:
Fetch's body = Axios' data Fetch's body has to be stringified, Axios' data contains the object Fetch has no url in request object, Axios has url in request object Fetch request function includes the url as parameter, Axios request function does not include the url as parameter. Fetch request is ok when response object contains the ok property, Axios request is ok when status is 200 and statusText is 'OK' To get the json object response: in fetch call the json() function on the response object, in Axios get data property of the response object.
它们是HTTP请求库…
我最终也产生了同样的怀疑,但这篇文章中的表格让我选择了同构取回。它是fetch,但适用于NodeJS。
http://andrewhfarmer.com/ajax-libraries/
上面的链接失效了 同样的表格在这里:https://www.javascriptstuff.com/ajax-libraries/
或者在这里:
根据mzabriskie在GitHub上的说法:
总的来说,它们非常相似。axios的一些好处: 变形:允许在发出请求之前或在接收到响应之后对数据进行转换 拦截器:允许您完全更改请求或响应(包括头)。另外,在请求被发送之前执行异步操作 在承诺解决之前 内置XSRF保护
请检查浏览器支持Axios
我认为您应该使用axios。
对于fetch,我们需要处理两个承诺。使用axios,我们可以直接访问响应对象数据属性中的JSON结果。
推荐文章
- 使用jQuery以像素为整数填充或边距值
- 检查是否选择了jQuery选项,如果没有选择默认值
- Next.js React应用中没有定义Window
- 如何重置笑话模拟函数调用计数之前,每次测试
- 如何强制一个功能React组件渲染?
- 在javascript中从平面数组构建树数组
- 将Dropzone.js与其他字段集成到现有的HTML表单中
- 如何在AngularJS中观察路由变化?
- JavaScript DOM删除元素
- 将dd-mm-yyyy字符串转换为日期
- Javascript复选框onChange
- Javascript函数前导bang !语法
- 如何在页面上遍历所有DOM元素?
- 在JS/jQuery中触发按键/按键/按键事件?
- 如何每5秒重新加载页面?