When we create a web application, it is inevitable that we will have to create something from the data entered by the user. It could be creating a new user account when someone submits their information through a registration form or creating a new recipe for your awesome new recipe organizer app. Passing user data in some form to a new instance of a recipe saved in the database requires an HTTP POST request.
A POST request is what sends form data to the server where it can be created in a back end or API and saved to a database. By default, submitting a form requires a redirect or page refresh. While this is great for small applications, we want to provide a faster loading experience so bypassing this behavior
Using the jQuery post () allows us to make the HTTP request, receive a response and choose how to display the data all without redirect or update! Let ’s take a closer look at what post () should do all of this.
A closer look post ()
HTTP requests are like the game. The browser sends a request to the server through the API endpoint and the server returns a response. What message () is actually sending data to the end point will be created and saved in the basic data of the application.
Taking the above into consideration, we can assume post () accepts arguments, right? Law! post () takes up to four arguments, but for our purposes we focus on the two main ones: the destination URL and the data we send to that destination.
$. post (’/ recipes’, data)
Let’s take the example of our recipe organization app, we’ll start with our selector jQuery $. By invoking the post (), we send our data to the URL of ’/ recipes. So far so good. We have sent data to our server. Now we are preparing to receive the answer!
What we are returned as a response is something called a jQuery XHR or jqXHR, which implements the Promise interface. Do not worry! the last sentence may sound scary, it tells us that the answer is just an object. With convenient jQuery done (), this object can be organized into a more readable format
to continue with our game of by analogy, we send the data to the server with post () . We returned a jqXHR, which we pass to done () so that we can see what we captured. Now that we have taken a closer look at post (), let’s see how to use it.
jQuery post ()
To start, we have a simple HTML form:
which displays the module as such:
From this callback function, we can stop the default redirect with preventDefault () . Now we are no longer and we will be redirected can start sending data to the server.
Before we call post () data post, we need data! Where can we get this data from?
We get the data from the form. Since we are no longer going to be redirected, we have access to the values ‚Äã‚Äãin the text inputs of the form. What’s going on with this serialize ()?
The serialize () method is called in the form we selected with the keyword this. What is serialize () This converts the form data into a standard URL code text string , which can be sent to the server.
Once we have successfully serialized our form data, we store in a variable:
Now we have our data stored in the values ​​variable, now we can send it to the server p! >
Remember that post () returns the jqXHR? We can also save as a variable and call done () on it to decompress the response and handle as we choose on our client side.
After receiving the data and stored in a variable more readable than recipe, we can access the name and attributes of the ingredient. As an example, we have placed these attributes in a
a common use of post () is in .submit () as a callback function. Since .submit () is called in the form itself, we can use the JavaScript keyword this to refer to all data in the form and serialize () . the data serialized into a variable, often called values, we can send those values ​​to our destination url using post ().
It is good practice to store the jqXHR object returned by post () in a variable commonly known as display. from there, we use the Made scope of main () on our display variable and now we have access to server data has responded. now it is up to you how to display the data to your user.
Pick up a mo I’m lying to congratulate you on going through the complex post () process! If you are at this stage of building your application, remember that errors do occur. Keep calm and debug!
We hope this article has helped you to resolve the problem. Apart from Javascript Sends Mail Request, check other __main__ Python module-related topics.
keyword Python module is always a bit confusing 😭 Javascript Sends Mail Request is not the only problem I encountered. Will use it in my bachelor thesis
Anna Robinson
Munchen | 2023-02-03
Simply put and clear. Thank you for sharing. Javascript Sends Mail Request and other issues with io Python module was always my weak point 😁. Will get back tomorrow with feedback
Angelo Emmerson
Rome | 2023-02-03
keyword Python module is always a bit confusing 😭 Javascript Sends Mail Request is not the only problem I encountered. Will get back tomorrow with feedback
Shop
Learn programming in R: courses
$FREE
Best Python online courses for 2022
$FREE
Best laptop for Fortnite
$399+
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?