JQuery Ajax example

Below are few example of how to use ajax in jquery. You can send ajax request by get method or by post method.

Request “index.php”, but ignore return results:


$.get("index.php");

Request “index.php” and send some additional data along with the request (ignore return results):

$.get("index.php", { name:"John", town:"Ducktown" });

Request “index.php” and pass arrays of data to the server (ignore return results):

$.get("index.php", { 'colors[]' : ["Red","Green","Blue"] });`

Request “index.php” and alert the result of the request:


$.get("index.php", function(data){
alert("Data: " + data);
});


var url = "url-of-script-goes here"
var data = {};
data.id = 1;
data.name = "john";

$.ajax({
type: 'POST',
url: url,
data: data,
dataType: 'text',
success:function(result) {

},
error: function (jqXHR, exception) {

},
complete: function (data) {

}
});

Leave a Reply

Your email address will not be published. Required fields are marked *

19 − 8 =