# Wednesday, 29 January 2014

Ajax refers to the pattern of calling server-side methods from client-side JavaScript. jQuery provides a simple, straightforward method for making Ajax calls. The syntax is

$.ajax({
            url: ServiceEndpoint,
            dataType: ReturnDataType,
            type: HttpVerb,
            data: Data,
            error: function (err) {
                // Code to run when error returned
            },
            success: function (data) {
                // Code to run when successfully returned
            }
          });

where

  • ServiceEndpoint is the URL of the method to call on the server
  • ReturnDataType is the data format we expect the server method to return (“xml”, “html”, “script”, “json”, “jsonp”, or “text”). You can specify multiple values and the server will return the first matching format type that is supported by this method.
  • HttpVerb is the HTTP verb (“GET”, “POST”, “PUT”, or “DELETE”) to use to send data to the server.
  • Data is the data (if any) that is sent from the client to the server.

By default the Ajax method executes asynchronously. When a call returns from the server, jQuery will run the function specified in the success parameter (if the call returned successfully); or the function in the error parameter if an exception occurred. These functions accept return data or error information returned from the server as parameters, so that your client-side code can handle return values effectively.

Ajax can provide a much more responsive experience to your web page and jQuery can make ease the process of making Ajax calls.