Ajax has made it easier to send and retrieve data from a server asynchronously without interfering with the existing page’s display and functionality.
Ajax is not a programming language and requires a built-in browser with the XMLHttpRequest object, which requests data from the server. It also uses JavaScript and HTML DOM to display the data.
The Ajax post method sends an asynchronous request to the server to load data.
Its syntax is:
jQuery.post(url, data, callback, dataType)
Where:
url
- is the address to which the request should be sent. It is mandatory.data
- a plaintext object or string sent to the server.callback
- an optional function that is executed if the request succeeds.dataType
- the type of data expected from the server. If this value is added, the callback must also be defined.Let’s look at an example – first, we need to introduce some HTML + jQuery text:
We need to define our post.asp
file, which is also the first parameter of our .post
method. This file also refers to the URL we wish to request.
// post.asp
<%
dim name
name=Request.Form("name")
Response.Write("Hello " & name & ". Welcome to Educative.")
%>
When you click on the welcome
button, this post.asp
is executed, and the button is replaced with the following data.