Editing
Making AJAX Requests In React
Jump to navigation
Jump to search
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
[[Category:React]][[Category:Web Development]] == Overview == Notes on how to use an AJAX request to fill out the state of a React component. == AJAX libraries == It's necessary to use a library to make AJAX calls. These libraries are independent of React. They are installed with NPM. <ref>[https://daveceddia.com/ajax-requests-in-react/ AJAX Requests in React: How and Where to Fetch Data] - Dave Ceddia</ref> * [https://github.com/mzabriskie/axios Axios]<br />Apparently Axios is a little more compact than fetch, e.g. with fetch it's necessary to convert AJAX responses to JSON; with Axios that step is automatic. And error handling is supposed to be better with axios.<ref>[https://medium.com/@thejasonfile/fetch-vs-axios-js-for-making-http-requests-2b261cdd3af5 Fetch vs axios.js for making http requests] - Medium</ref> * [https://github.com/visionmedia/superagent superagent] * [https://github.com/github/fetch fetch] == Usage == An example of how to make an AJAX call to retrieve JSON data using axios. The data retrieved is a JSON object (or list of objects), not a JSON string. <syntaxhighlight lang="js"> axios.get("/path/to/content/") .then(res => { const data = res.data; }) .catch(err => { console.log("Something went wrong: " + err); }); return data; </syntaxhighlight> == Notes == <references />
Summary:
Please note that all contributions to Littledamien Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Littledamien Wiki:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Namespaces
Page
Discussion
English
Views
Read
Edit
View history
More
Search
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Tools
What links here
Related changes
Special pages
Page information