Lec #5 - Jquery Ajax
Lec #5 - Jquery Ajax
jQuery - Ajax
AJAX is an acronym standing for Asynchronous JavaScript and XML and this technology helps us to load data
from the server without a browser page refresh.
Syntax
Here is the simple syntax for load() method –
[selector].load( URL, [data], [callback] );
$(document).ready(function() {
$("#driver").click(function(event){
$('#stage').load('result.html');
Here load() initiates an Ajax request to the specified URL /jquery/result.html file. After loading this file, all the
content would });be populated inside <div> tagged with ID stage. Assuming, our /jquery/result.html file has just
one HTML line −
});
<h1>THIS IS RESULT...</h1>
When you click the given button, then result.html file gets loaded.
</script>
</head>
<body>
</html>
DCIT111 – Advanced Programming hayperaktib
Lecture # 5 [jQuery Ajax]
Syntax
Here is the simple syntax for getJSON() method –
Example
Consider the following HTML file with a small JQuery coding –
<html>
<head>
$(document).ready(function() {
$("#driver").click(function(event){
$.getJSON('/jquery/result.json', function(jd) {
});
});
});
2
DCIT111 – Advanced Programming hayperaktib
Lecture # 5 [jQuery Ajax]
Here JQuery utility method getJSON() initiates an Ajax request to the specified URL result.json file. After loading
this file, all the content would be passed to the callback function which finally would be populated inside <div>
tagged with ID stage. Assuming, our result.json file has following json formatted content –
"age" : "67",
When you click the given button, then result.json file gets loaded.
Example
This example demonstrate how can pass user input to a web server script which would send the same result back
and we would print it −
<html>
<head>
$(document).ready(function() {
$("#driver").click(function(event){
$("#stage").load('/jquery/result.php', {"name":name} );
});
});
</script>
3
DCIT111 – Advanced Programming hayperaktib
Lecture # 5 [jQuery Ajax]
<?php
if( $_REQUEST["name"] ){
$name = $_REQUEST['name'];
echo "Welcome ". $name;
}
?>
Now you can enter any text in the given input box and then click "Show Result" button to see what you have
entered in the input box.
1 jQuery.ajax( options )
Load a remote page using an HTTP request.
2 jQuery.ajaxSetup( options )
Setup global settings for AJAX requests.
8 serialize( )
Serializes a set of input elements into a string of data.
9 serializeArray( )
Serializes all forms and form elements like the .serialize() method but returns a
JSON data structure for you to work with.
4
DCIT111 – Advanced Programming hayperaktib
Lecture # 5 [jQuery Ajax]
1 ajaxComplete( callback )
Attach a function to be executed whenever an AJAX request completes.
2 ajaxStart( callback )
Attach a function to be executed whenever an AJAX request begins and there is
none already active.
3 ajaxError( callback )
Attach a function to be executed whenever an AJAX request fails.
4 ajaxSend( callback )
Attach a function to be executed before an AJAX request is sent.
5 ajaxStop( callback )
Attach a function to be executed whenever all AJAX requests have ended.
6 ajaxSuccess( callback )
Attach a function to be executed whenever an AJAX request completes
successfully.