JQuery Ajax Methods
The ajax() method is used to perform an AJAX (asynchronous HTTP) request .
JQuery has provided following methods to achieve AJAX function in a page.
1- JQuery load() Method:
The load() method loads data from a server and puts the returned data into the
selected element.
Syntax:$(selector).load(URL,data,callback);
The required
URL parameter specifies the URL you wish to load.
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
Example-
$("#div1").load("demo.txt", function(resText, statusText, resObj){
if(statusTxt == "success")
{
Write your logic here
}
if(statusTxt == "error")
{
Write your logic here.
});
2-JQuery get():GET is basically used for just getting (retrieving) some data from the server.
The $.get() method requests data from the server with an HTTP GET request.
Syntax:$.get(URL,callback);
The required
URL parameter specifies the URL you wish to request.
The optional
callback parameter is the name of a function to be executed if the request succeeds.
Example: $.get("demo.aspx", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
The first parameter of $.get() is the URL we wish to request ("demo.aspx").
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.
3-JQuery post() :The $.post() method requests data from the server using an HTTP POST request.
Syntax:$.post(URL,data,callback);
The required
URL parameter specifies the URL you wish to request.
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.
Example: $.post("demo.aspx",
{ name: “Neetu Rathor",
Company: “VirtalClasses.co.in",
}
function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
4-JQuery Ajax method-JQuery has provided Ajax method for ajax implementation on page.This method allow to give data type,type of data to be exchnaged with the server and whether it is a get or post request.
basically it is a combination of get and post. method property of ajax method specifies whether it is a ajax get or ajax post request.
$.ajax({
url: “demo.aspx",//path to be called
method: "POST",//method specifies get request or post request
data: { id : 1 },//data types to be passed if any
dataType: "html“ , // content type would be HTML/ JSON/ text
success: function(data)
{
//logic goes here
},
error: function(data)
{
//logic goes here
}
});