Jquery - Ajax Introduction
Jquery - Ajax Introduction
AJAX is the art of exchanging data with a server, and updating parts of a web page - without
reloading the whole page.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>
</body>
</html>
What is AJAX?
In short; AJAX is about loading data in the background and display it on the webpage,
without reloading the whole page.
Examples of applications using AJAX: Gmail, Google Maps, Youtube, and Facebook tabs.
With the jQuery AJAX methods, we can request text, HTML, XML, or JSON from a remote
server using both HTTP Get and HTTP Post - And we can load the external data directly into
the selected HTML elements of the web page!
Writing regular AJAX code can be a bit tricky, because different browsers have different
syntax for AJAX implementation. This means that we will have to write extra code to test for
different browsers. However, the jQuery team has taken care of this for us, so that we can
write AJAX functionality with only one single line of code.
The load() method loads data from a server and puts the returned data into the selected
element.
Syntax:
$(selector).load(URL,data,callback);
The optional data parameter specifies a set of querystring key/value pairs to send along
with the request.
The optional callback parameter is the name of a function to be executed after the load()
method is completed.
The following example loads the content of the file "demo_test.txt" into a specific <div>
element:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt");
});
});
</script>
</head>
<body>
</body>
</html>
The following example loads the content of the element with id="p1", inside the file
"demo_test.txt", into a specific <div> element:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").load("demo_test.txt #p1");
});
});
</script>
</head>
<body>
</body>
</html>
The optional callback parameter specifies a callback function to run when the load() method
is completed. The callback function can have different parameters:
The following example displays an alert box after the load() method completes. If the load()
method has succeeded, it displays "External content loaded successfully!", and if it fails it
displays an error message:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
if(statusTxt == "success")
if(statusTxt == "error")
});
});
</script>
</head>
<body>
</body>
</html>
The jQuery get() and post() methods are used to request data from the server with an HTTP GET
or POST request.
Two commonly used methods for a request-response between a client and server are: GET
and POST.
GET is basically used for just getting (retrieving) some data from the server. Note: The GET
method may return cached data.
POST can also be used to get some data from the server. However, the POST method
NEVER caches data, and is often used to send data along with the request.
The $.get() method requests data from the server with an HTTP GET request.
Syntax:
$.get(URL,callback);
The optional callback parameter is the name of a function to be executed if the request
succeeds.
The following example uses the $.get() method to retrieve data from a file on the server:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
});
});
});
</script>
</head>
<body>
<button>Send an HTTP GET request to a page and get the result back</button>
</body>
</html>
The second parameter is a callback function. The first callback parameter holds the content
of the page requested, and the second callback parameter holds the status of the request.
<%
response.write("This is some text from an external ASP file.")
%>
jQuery $.post() Method
The $.post() method requests data from the server using an HTTP POST request.
Syntax:
$.post(URL,data,callback);
The optional data parameter specifies some data to send along with the request.
The optional callback parameter is the name of a function to be executed if the request
succeeds.
The following example uses the $.post() method to send some data along with the request:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
city: "Duckburg"
},
function(data,status){
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
Then we pass in some data to send along with the request (name and city).
The ASP script in "demo_test_post.asp" reads the parameters, processes them, and returns
a result.
The third parameter is a callback function. The first callback parameter holds the content of
the page requested, and the second callback parameter holds the status of the request.
<%
dim fname,city
fname=Request.Form("name")
city=Request.Form("city")
Response.Write("Dear " & fname & ". ")
Response.Write("Hope you live well in " & city & ".")
%>