$.Ajax ( server_data_json )
AJAX implementation is often complex and browser specific. NedilJS AJAX apis are simpler and removes the complexities invloved.
$.Ajax takes server url and cache as inputs and returns the object reference of ajaxFactory.
Parameters
Returns
$.domLoaded(function() { var ajax = $.Ajax({ url : "SERVER_URL", cache : false }); }); .send( request_data_json ) on ajaxFactory object
send() function will be used on ajaxFactory object. This function makes the actual AJAX request to the server and recieves the response. It takes data, type and handlers as JSON input parameter.
Parameters
$.domLoaded(function() { var ajax = $.Ajax({ url : "SERVER_URL", cache : false }); ajax.send({ type : "GET", datatype : "text", data : { "name" : "Senthil", "age" : 24}, async : true, success : function(res) { console.log(res); }, fail : function() { console.log("failure"); } }); }); Example 1 - Text
This example sends name and age data to the server and server responds with text format.
JS
$.domLoaded(function() { $.get("#ajax1").on("click", function() { $.Ajax({ url : "../../ajax/AjaxServer.php"}).send({ data : { name : "Senthil", age : 24 }, success : function(res) { $.get("#result").html(res); }, fail : function() { $.get("#result").html("Some error has occured"); } }); }); });
Result
Example 2 - XML
This example sends name and age data to the server with xml datatype and server responds with xml format.
JS
$.domLoaded(function() { $.get("#ajax1").on("click", function() { $.Ajax({ url : "../../ajax/AjaxServer.php"}).send({ data : { name : "Senthil", age : 24 }, datatype : "xml", success : function(res) { var root = res.documentElement; var name = root.getElementsByTagName("name")[0]; var age = root.getElementsByTagName("age")[0]; $.get("#result").html("Name : " + name.innerHTML + "
Result
Example 3 - JSON
This example sends name and age data to the server with JSON datatype and server responds with JSON format.
JS
$.domLoaded(function() { $.get("#ajax1").on("click", function() { $.Ajax({ url : "../../ajax/AjaxServer.php"}).send({ data : { name : "Senthil", age : 24 }, datatype : "json", success : function(res) { $.get("#result").html("Name : " + res.name + "
Result
| |||||||||||||||||||||||||||||||||||||||