$.ajax in jQuery

AJAX and jQuery

Just in breve as a powerful JavaScript library jQuery wraps the ability to call XHR within a built in interface. There are several ways to implement asynchronous calls. In fact you can call either .load or .post to perform custom calls.

$.load

In the case of .load you can chain it on every returned object from a custom jQuery selector: $(‘div’).load(.. than the content of the DIV is filled with the returned text.

But what if I want a OK and Error handlers?

The .load method doesn’t give such “complex” functionality. Than the built in $.ajax method comes in help. You can specify the URI, method, data, dataType, onSuccess, onError handlers, etc.

There you have:

$.ajax({
   url : 'www.example.com', // string
   data : { param1 : 'value1', param2 : 'value2' },
   // will result in www.example.com/?param1=value1&para...
   dataType : {json|html|script},
   success : function(text) {
      // where text is returned text
   },
   error : function() { // handle errors }
});

Well of course you can find the full spec on jQuery docs page. What I intended to do is that $.ajax function gives you the real power to call different type of data types and to handle the responses in different fine tuned ways.

Leave a Reply

Your email address will not be published. Required fields are marked *