I am calling the web service by using fetch but the same I can do with the help of axios. So now I am confused. Should I go for either axios or fetch?
相关问题
- How to toggle on Order in ReactJS
- Carriage Return (ASCII chr 13) is missing from tex
- Using :remote => true with hover event
- Refreshing page gives Cannot GET /page_url in reac
- Is there a way to play audio on a mobile browser w
相关文章
- spring boot用ajax发送请求后,请求路径多了controller的路径
- 针对复杂结构的前端页面,如何更好地与后台交互实现动态网页?
- ajax上传图片,偶尔会出现后台保存的图片有错误或者已损坏,请问可能是什么原因造成的?
- 前端 我想知道怎样通过发ajax请求向服务器拿到数据然后分页显示 最好是点击一页就发一次请求
- 接口返回的数据格式如下,请问可以取到level值为2的name数组呢
- 如何通过页面输入账号密码提交给后端
- How to get jQuery.ajax response status?
- Why would we use useEffect without a dependency ar
Was curious about this too, found your question here, then found this Medium post which may prove helpful. Fetch seems a little more verbose and unforgiving: https://medium.com/@shahata/why-i-wont-be-using-fetch-api-in-my-apps-6900e6c6fe78#.wxtu3qnte
In addition... I was playing around with various libs in my test and noticed their different handling of 4xx requests. In this case my test returns a json object with a 400 response. This is how 3 popular libs handle the response:
Of interest is that
request-promise-native
andaxios
throw on 4xx response whilenode-fetch
doesn't. Alsofetch
uses a promise for json parsing.Fetch and Axios are very similar in functionality, but for more backward compatibility Axios seems to work better (fetch doesn't work in IE 11 for example, check this post)
Also, if you work with JSON requests, this are some differences I stumbled upon with.
Fetch JSON post request
Axios JSON post request
So:
Hope this helps.
According to mzabriskie on GitHub:
I think you should use axios.
axios and fetch are frequently used in React applications for managing network requests and managing some amount of data.
axios is a stand-alone 3rd party package that can be easily installed into a React project using NPM.
The other option you mentioned is the fetch function. This is not a package, its a singular function built into most modern browsers. With fetch you do not need to install a third party package.
So the amount of code is slightly smaller if you use fetch over axios. However, fetch is a more basic and lower level function to use to fetch data. Its not the worst thing in the world, but if you use fetch you will end up writing a lot of code that is already written for you in axios.
So its a choice between a D.I.Y. with fetch and possibly messing up if you don't know what you are doing OR just use axios and all the nuts and bolts are taken care of for you.
I personally use axios on all of my projects because axios handles network requests in a predictable fashion as opposed to some corner cases with fetch that don't make for a great experience.
They are HTTP request libraries...
I end up with the same doubt but the table in this post makes me go with
isomorphic-fetch
. Which isfetch
but works with NodeJS.http://andrewhfarmer.com/ajax-libraries/