JQuery Ajax Basics

This example give you basic ajax call in MVC deom.

$.ajax({
    type: ‘Get’,
    url: ‘ControllerName/MethodName’,
    data: { Parameter1: ‘12345′, Parameter2: ‘12345′ },
    async: false,
    dataType: “html”,
    success: function (signature) { 
        if (signature != ”){
            // do whatever you want
        }
    },
    error: function (xhr, ajaxOptions, thrownError) {
       alert(xhr.status);
       alert(thrownError);
    }
});
Addition to this

Since all the Ajax interactions happen behind the scenes asynchronously, the user doesn’t understand what’s going on. That is time between ajax request and ajax response. Some tine it will take time to response, where you can show loading data image or please wait request is in progress or progress bar.
Using jquery you can do it very easily with the help of ajaxStart and ajax ajaxStop function

ajaxStart()

Whenever an Ajax request is about to be sent, jQuery checks whether there are any other outstanding Ajax requests. If none are in progress, jQuery triggers the ajaxStart event. Any and all handlers that have been registered with the .ajaxStart() method are executed at this time.

$(document).ajaxStart(function () {
    $(‘#loadingDiv’).show();
})

This function will execute whenever you give ajax request on this page.
ajaxStop()

Whenever an Ajax request completes, jQuery checks whether there are any other outstanding Ajax requests. If none remain, jQuery triggers the ajaxStop event. . Any and all handlers that have been registered with the . ajaxStop () method are executed at this time.

$(document).ajaxStop(function () {
    $(‘#loadingDiv’).hide();
})
Advertisements
This entry was posted in Ajax, jQuery and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s