Are there progress update events in jQuery ajax?

前端 未结 6 782
北海茫月
北海茫月 2020-12-31 16:22

i have long running task that gets called using jquery ajax. i am using the block ui plugin to show \"loading\". is there anyway i can send progress messages back to the

6条回答
  •  悲哀的现实
    2020-12-31 16:59

    One way is to use HTTP Handlers and AJAX with jQuery.

    1. Initiate Server side request

    $("#btnCreateInvoice").click(function() {             
           $.ajax({ type: "POST",  url: "YourHttpHandler.ashx",
           contentType: "text/html; charset=utf-8",
           dataType: "html",  
           success: function(data) { start the block UI }
           });
        });
    

    2. Polling

    What next you need to do is to poll the server at 't' interval and get the status. For that we need to call a function at 't' interval that would initiate an AJAX call to a HTTPHandler to get the status.

    $(function() {
      setInterval(updateStatus, 't');
    });
    
    function updateStatus() {
      $.ajax({ type: "POST",  url: "GetStatusHandler.ashx",
           contentType: "text/html; charset=utf-8",
           dataType: "html",  
           success: function(data) { process 'data' here and update the block UI message box }
           });
    }
    

    In your case here, the GetStatusHandler.ashx can return the complete innerHtml for status. For eg on the first call it will return 'Loading First source...', then it might return:
    Loding First source...
    Loading Second source...
    and so on.

提交回复
热议问题