$.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.

Related posts:

  1. AJAX dataTypes in jQuery. Format and access.
  2. JQuery ajax script call
  3. jQuery – stop an ajax call!
This entry was posted in javascript, micro tutorial and tagged , , , . Bookmark the permalink.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">