As AJAX continues to take over the Web world and dominate in the Web 2.0 revolution, there are so many AJAX libraries that come to mind. Fortunately for us jQuery comes to our rescue providing a small footprint with an enormous set of features & plugins, but also is about the easiest library out there to use.
jQuery makes life so much easier. Look at the following example to see a perfect example:
function SendDataSomeWhereUsingAJAX(stringOfData)
{
$.ajax({
type: "POST",
url: "some.php",
data: "VariableIWantToSend=stringOfData",
success: function(msg) {
alert( "Data Saved: " + msg );
}
});
}
This is a basic AJAX POST in jQuery. Lets analyze what's really going on here!
1 - We have a function called "SendDataSomeWhereUsingAJAX" which can be called from just about anything!
2 - $.ajax() is the jQuery Method for starting an AJAX call (Either POST or GET).
3 - Inside the $.ajax() method we have an array of method parameters being passed in (example only shows SOME of the possible arguments!).
4 - type: This is where you will set POST or GET.
5 - url: the URL of the script this data is being sent too.
6 - data: a string which is in variable=value form. This string contains all of the data being sent which will be parsed into a POST or GET.
7 - success: this parameter defines what should happen if the request succeeds. Also notice in the success parameter function we have an argument "msg" which is the data that the AJAX script returned with. You can make this variable what ever you. MSG was used for demonstration purposes only.
This is as easy as it gets with AJAX :-)