With traditional HTML and JavaScript, to get some data from the server side, it was necessary to make a form submission. With each submission the entire form was being reloaded. So it was loading an entirely new page and that was making performance slow.

By 2003, all the major browsers adopted the XMLHttpRequest (XHR) object, that allowed browsers to communicate with the server directly without a page reload.

The JavaScript object “XMLHttpRequest” is actually part of AJAX technology. Using the XMLHttpRequest API, JavaScript can directly interact with the server. The user can make a request to the server with a HTTP Get Response from the server and modify the required portion only (without a form submission/reload). But, various browsers implement it differently. For example Internet Explorer uses an ActiveXObject whereas other modern browsers use the built-in “XMLHttpRequest” JavaScript object.

So it's an overhead for developers to make implementation of XMLHttpRequest universal. To create an object that can call Server-side functionality directly we need to consider other browsers like:

  1. function CreateAsyncObject()
  2. {
  3. var ObjXmlHttp;
  4. try
  5. {
  6. ObjXmlHttp = new XMLHttpRequest();
  7. }
  8. catch(Exception OuterEx)
  9. {
  10. try
  11. {
  12. ObjXmlHttp = ActiveXObject(“Msxml2.XMLHTTP”);
  13. }
  14. catch(Exception Ex)
  15. {
  16. try
  17. {
  18. ObjXmlHttp = ActiveXObject(“Microsoft.XMLHTTP”);
  19. }
  20. catch(Exception ExInner)
  21. {
  22. alert(“Your browser doesn't support XMLHttpRequest.”);
  23. }
  24. }
  25. }
  26. }
Then we need to open this object with the required parameters and send it to the server like:
  1. ObjXmlHttp.open(“<GET/POST>”, “<Server side url>”, <true/false>);
The last parameter indicates whether the request is to be handled asynchronously.
  1. ObjXmlHttp.send(null);
Properties of XMLHttpRequest

A complete request using XMLHttpRequest will be as follows:

  1. <input type= “text” onkeyup = “GetName()”>
  2. <div id= “divResult”></div>
  3. function GetName()
  4. {
  5. var ObjXmlHttp;
  6. try
  7. {
  8. ObjXmlHttp = new XMLHttpRequest();
  9. }
  10. catch(Exception OuterEx)
  11. {
  12. try
  13. {
  14. ObjXmlHttp = ActiveXObject(“Msxml2.XMLHTTP”);
  15. }
  16. catch(Exception Ex)
  17. {
  18. try
  19. {
  20. ObjXmlHttp = ActiveXObject(“Microsoft.XMLHTTP”);
  21. }
  22. catch(Exception ExInner)
  23. {
  24. alert(“Your browser doesn't support XMLHttpRequest.”);
  25. }
  26. }
  27. }
  28. ObjXmlHttp.open(“GET”, “GetName.aspx”, true);
  29. ObjXmlHttp.onreadystatechange(function(){
  30. if(readyState == 4)
  31. {
  32. divResult.innerHTML = ObjXmlHttp.responseText;
  33. }
  34. });
  35. }
jQuery-Ajax

jQuery provides Ajax support that removes all the overhead of creating objects (considering various browsers), handling a “readystatechange” event, checking readyState and so on. By some full-featured method like $.ajax() and some convenience methods like $.get(), $.post() and so on.

There are many $.ajax() Options available. Some of the most common properties are:

Option Description Default value
async If the request should be served asynchronously (true)
url URL to request. This is the only required value to make an Ajax call
contentType Type of content query is ed to the server application/x-www-form-urlencoded;charset=UTF-8
data Data query is ed to the server
dataType Type of data jQuery is expecting from server MIME type of response
success Success callback
error Error callback
complete Code to execute irrespective of success/failure
crossDomain It indicates if request is to/from cross-domain (false)

NOTE: If you are not sure about ContentType and dataType, just don't mention them in the configuration of a jQuery Ajax call, It'll take default values that will satisfy most of the types.

  1. $.ajax({
  2. type: "POST",
  3. url:"http://localhost/WebServiceForBlog/MyService.asmx/SumOfNums",
  4. data: "First=" + FirstNum + "&Second=" + SecondNum,
  5. contentType: "application/x-www-form-urlencoded;charset=UTF-8"
  6. dataType: "text",
  7. crossDomain: true,
  8. success: function (data) {
  9. $('#divSum1').html(data);
  10. }
  11. error: function(data)
  12. {alert("Error");}
  13. });
Let's discuss one of the most commonly used callback functions.

error

Success

These jQuery methods use the XMLHttpRequest API internally for calling server-side functionalities directly.

By default, Ajax doesn't support cross-domain calls since it violates “Same origin policy”. JSONP (JSON with padding) that uses Cross-Origin Resource Sharing can be used for cross-domain calls.