The people of Nettuts publishes an interesting article on using jQuery with Ajax. Specifically the 5 ways that jQuery allows us to send asynchronous requests.
- $.load()
- $.get()
- $.post()
- $.getJSON()
- $.getScript()
$. load ()
This is the feature I like most about jQuery already makes one of the most common tasks of developing with Ajax is as simple and straightforward as we shall see in this example:
$("#div_links").load("/Main_Page #jq-p-Getting-Started li");
This example brought to the documentation page on jQuery, it manages to launch a request to the URL /Main_Page (usa URL Rewrite) HTML response and take the #jq-p-Getting-Started li and inserted into #div_links
Just great, comfortable and fast.
$. get ()
This is the simple function with which we can launch the server via GET requests Ajax.
$.get("test.cgi", { name: "Venu", time: "7pm" },
function(data){
alert("Data Loaded: " + data);
});
By passing 3 options, 2 of which are options (rather conditional) you can request to launch the file (1) with parameters (2) and treat the response through a callback (3rd).
$. post ()
Like the above, this feature lets you send POST requests using Ajax.
$.post("test.php", { name: "Venu", time: "7pm" },
function(data){
alert("Data Loaded: " + data);
});
As easy as above.
$. getJSON ()
Although the above are able to specify the type of return, the best option is to use this method to obtain the response in JSON evaludada the callback function.
$.getJSON("test.js", { name: "Venu", time: "7pm" }, function(json){
alert("JSON Data: " + json.users[3].name);
});
Taking into account the browser, the object will use native JSON or use a system based on eval().
$. getScript ()
Although technically not an Ajax request is a request to the server and therefore has a place in the post.
$.getScript("test.js", function(){
alert("Script loaded and executed.");
});
With this method we can load a file asynchronously using JavaScript and the parameter (2) callback can execute Javascript code that is using the js file that we want to load (1).
