Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery UI dialog showing waiting message until ajax call is processed

I think that all is in the title. When I started this I thought it will be a 5 minutes codes or a quick result when googling... But It's now three hours I'm on this:

Just show a dialog, with a "Please Wait..." message, while I'm performing an ajax call to retrieve some json result, and then show a "Result complete".

$('#switchOff').live("click",function(){

   $('#dialog').dialog({
                modal:true,

               open: function(){
// I would like to call myAjax function
//From here ?
// While my dialog is showing the Wait message...
               },


                complete: function(){
//close the dialog when fished
                 $('#dialog').dialog('close');
                         },
   }); 



});

function ajaxCall() {
                //my ajax call
}
like image 277
Zamboo Avatar asked Apr 04 '13 16:04

Zamboo


1 Answers

You should not use live anymore. Use .on instead. You are mixing up the dialog an ajax code. Here is an example of how I would do it.

Here is link to fiddle for demonstation

$('#switchOff').on("click",  ajaxCall);

$("#loading").dialog({
    hide: 'slide',
    show: 'slide',
    autoOpen: false
});

function ajaxCall() {
    $.ajax({
        url: '/echo/html/',
        data: { html: '<p>JSON result</p>' , delay: 3},
        method: 'post',
        beforeSend: function(){
           $("#loading").dialog('open').html("<p>Please Wait...</p>");
        },
        success: function(data) {
            $('#loading').html("<p>Result Complete...</p>");
            $('#ajaxResult').html(data);
        }
    });
} 
like image 147
jmm Avatar answered Nov 26 '22 06:11

jmm