Introduction to AJAX in JQuery

AJAX, jQuery 6 responses so far

At first, I should define, what is AJAX? The term refers to the use of different technologies like XML, XHTML, and JavaScript to update a part of a web page without reloading the whole page. This feature is becoming an essential part of modern web development.

In this regard, modern browsers provide XMLHttpRequest API interface for sending requests directly to a web server and load the server response data back into the JavaScript. Working with this API is not convenient for most web developers. Hence, libraries like JQuery try to wrap this API and allow one to easily add Ajax to his or her application. And in this tutorial I am trying to show how one can use it in JQuery.

The main function for using Ajax in JQuery is jQuery.ajax(). There are other methods like post(), and get(), to name but two, to send Ajax requests, but at their cores they use ajax() function. Therefore, in this tutorial, we only discuss about ajax() function.

In the simplest form one can call it just by specifying url setting:


  url: "./test.php"


this fuction call results in sending a HTTP GET to the requested url which is ./test.php. However most of the time on use it like the following:

        type: "POST",
        cache: false,
        contentType: "application/json; charset=utf-8",
        url: './test.php',
        data: JSON.stringify(pdata),
        dataType: "json",
        async: true,
        success: function (data, textStatus, jqXHR) {
            if (textStatus == "success") {

        error: function (jqXHR, textStatus, errorThrown) {

These are the most useful settings that one can use. By setting type you can specify by which HTTP command you want to send your HTTP request. You can set either POST or GET. I think the POST value is the most used one. However, the default value for the type setting is GET. The next useful setting is cache, which by using it you can force requested pages not to be cached by the browser. Set it to false when the content of the page you are calling is not static and will change in each call. The default value for this setting is true. If the page is dynamic make sure that it is false.

if one set the type to POST, he or she can send data back to the server in the HTTP body. To do so, one can use the following settings: contentType, data, dataType. I will talk more about them in the next tutorial.

Another setting that one can set is async. When it is set to false, the execution will be blocked until the response arrives. If it is set to true, which is the default value, the request is asynchronous and the execution will continue.

The success function to be called if the request succeeds. The function gets passed three arguments: The data returned from the server, formatted according to the dataType parameter; a string describing the status; and the jqXHR object.

The error function to be called if the request fails. The function receives three arguments: The jqXHR object, a string describing the type of error that occurred and an optional exception object, if one occurred.

If one wants to change the default values can call ajaxSetup function with those settings that wants to override before calling ajax function. For example like this:

    cache: false

I think it suffice for today. In the next tutorial I will try to show you a real example by sending and receiving data  from a WCF web service.

The following two tabs change content below.


Latest posts by Amir (see all)

  • I read your post and I found this is amazing.Your thought process is wonderful. The way you express yourself is awesome.

  • Thanks a lot for explanation about POST and GET function. Sometimes I get confusion about those, but now I am very clear about both.

  • louis vuitton bags outlet store

    With deer skin, its tricky, it has always been tricky, as its been known to bleed. AND PRADA IS NOT POOR WORKMANSHIP, it is a respected italian leather house, steeped in history, as much as LV is.

  • I like how you explain things; you keep it simple, omitting bluster without omitting details. I will probably come back to this tutorial when I want to get started with AJAX.

  • To simplify the AJAX components you can use a library like JQuery. If you are using the Twitter CSS Bootstrap toolkit then JQuery will already be added in.

  • Very Informative post and it quite helpful to me. Thanks for sharing