Tuesday 7 September 2010

Calling AJAX with JQuery - Accepted parameters

Calling AJAX with JQuery

AJAX permit us to make calls to a webservice from Javascript. Bear in mind that the Javascript code is executed in the client computer.

Let's see how make an AJAX call using JQuery.

In the Head of our HTML code we have to link the JQuery library. (Google offers us without need to download or add to our project)

src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.js"

In the Javascript code:
The code base to make a call is the follow:

//Option 1.

$.ajax({
type: "GET",
url: URL,
success: allOK(msg),
error: ajaxError
}
);
//function that will be executed if the AJAX call is succesfull
function allOK(data){
alert(data);
}
//function that will be exected if the AJAX call fail
funtcion ajaxError(result){
alert("ERROR: " + result.status + ' ' + result.statusText);
}


//Opction 2.

$.ajax({
type: "GET",
url: URL,
success: function(msg) {
alert(msg);
},
error: function(result){
alert("ERROR: " + result.status + ' ' + result.statusText);
}
);

We can use the 2 notation types to treat the result.

1: Trating the result outside of the call using functions.
2: TTreating the result inside of the call.

A table with all parameter accepted for a AJAX call with the according description attached below.









Regards.

0 comments: