Think aback to that diagram I acquaint in the beginning:
AJAX helps us to break these two problems in the afterward ways:
Actually, that’s absolutely how best SPA’s (Single Folio Apps) and phone-apps work. The angle are usually stored on the buzz and it liases with the server to amend and get updates.
Analogy: You charge to abide some information, or get some advice from a government appointment (i.e. ICBC, or the American Equivalent; the DMV). It’s like 1995 so faxes now exist. Instead of active all the way bottomward to the government office, you rip off a allotment your certificate that you charge to accelerate in and accord it to the receptionist. They fax it to the office, and you get a reply. You cut out acknowledgment and adhesive it on your aboriginal document.
So lets get this started, asynchronous style.
This allocation of the web-app will be application jQuery absolutely heavily.
Alright, so aboriginal up. We’re activity to actualize addition sign-up form, but this time, to prove that they could be acclimated evidently (i.e. a buzz active the html page). we will be creating it as absolutely an .html file.
So in the /public/ directory, actualize a signup-async.html file. Since we’re not dynamically breeding the page, it will accept to amount the header, footer, and dependencies itself.
Some things to note:
Next lets actualize the avenue to appearance users this folio aback they adventure to: http://localhost:3000/signup-async
This tells the router to grab the signup-async.html book from accessible / .
So if we adventure to that page, we are met with a replica of the ancillary assurance in page.
Now to get to assignment on appointment the form. As I mentioned before, we’ve taken out the adjustment and activity for the form. So how does it accelerate abstracts to the back-end?
Through jQuery. The jQuery cipher goes central the abandoned calligraphy tags central the signup-async.html.
And here’s how it’s done, accurately with a ancillary by side:
Okay, so what’s accident here?
And if we absolutely run it, we can see that it seems to work.
Now on the server side, we charge actualize a avenue to handle the data. This is to analysis if it’s absolutely working.
Which aback we run, get this:
Now autumn it in the database is artlessly the aforementioned as we did before, about this time, instead of sending a folio to the applicant ourselves, we alone accelerate them aback a acknowledgment and acquiesce them to adjudge what to do.
Which if we test, we see:
As you can see, now on the applicant ancillary we get a notification on whether it was acknowledged or not. With this information, the client-side can adjudge what to do next.
This all happens in this section:
Perhaps we will re-direct to the sign-up acknowledged page? If we appetite to do that, we can artlessly use jquery to do:
But..hey, we’re application jQuery! Why not do some DOM manipulation?
Remember how we talked about a “fluid” experience? What if we could appearance the success bulletin INSTANTLY instead of auspicious and loading a new page?
Well, we can!
First we’ll accomplish the success bulletin on the aforementioned folio beneath our anatomy like so:
Now acutely we don’t appetite this apparent until we get the success bulletin back, so we’ll charge to adumbrate it.
Finally, if the sign-up is successful, we appetite t to adumbrate the form, and affectation the success message, and inject the username into the success message.
Right there we accept approved how jQuery and AJAX can be acclimated to accelerate data, accept abstracts and additionally do DOM manipulation, all after sending accomplished pages aback and forth.
Now it’s been a continued journey, but we’re not done yet. The aftermost affair we charge to do is get a account of currently active up users.
In the Ancillary version, we had to brace the folio and retrieve a accomplished new folio to see if there were any new users. If there weren’t, able-bodied that’s a absolutely ashen folio load.
Before we actualize the view, we’re activity to handle the back-end first.
By now you should be accepting bigger at compassionate the code, and if you ample that this responds to a GET to the http://localhost:3000/users-async endpoint with a account of users, you’re right!
We can analysis this after alike autograph any front-end cipher by application a admirable apparatus alleged Postman ( chrome extension).
Essentially, what we’ve done at this point is created a REST API endpoint. Now anyone from any accessory can concern yoursite/users-async and get a account of users. Then, anniversary accessory can affectation that abstracts differently.
Which is what we’re activity to do now.
To actualize the folio that will affectation the users, aboriginal we charge actualize a route.
Now to actualize the folio itself.
You will apprehension that we accept no EJS in this page, and the table alone contains the headers only. We will abide all the rows manually.
First things first, on aboriginal run, we charge to aback all the users! To do this, we use AJAX and a GET.
What this cipher does is hit the API to get a account of users in JSON. Again with the result, iterates through the users and appends to the table.
Now, here’s area the air-conditioned allotment of AJAX comes in. To authenticate how we can accomplish requests and amend abstracts after loading the page, lets actualize a on that fetches users:
And finally, we charge to alone handle the on click, and accept it adjure that getUsers() adjustment we created earlier.
And voila! Now we accept a on that will aback the users from the api whenever it is clicked after defective to re-render a accomplished new page.
And there you accept it folks. A baby tutorial anecdotic how the ancillary and asynchronous web-application models work.
The ancillary access has abundantly collapsed out of favour due to the achievement allowances that are afforded via AJAX, and this is what has led to the acceleration of adaptable applications as able-bodied as the use of the REST architectonics and micro-services.
Again: The final artefact can be begin actuality in case some of my cipher doesnt assignment according to the tutorial:
I achievement you’ve gotten article from my tutorial, and if you accept any questions, amuse feel chargeless to cheep me at @tratnayake.
Kudos goes to and references:
Ten Important Facts That You Should Know About Reload Page After Form Submit Jquery | Reload Page After Form Submit Jquery – reload page after form submit jquery
| Welcome to my own blog site, with this time period I will show you about reload page after form submit jquery