jQuery post request is not sent until first post request is compleated

Posted by Champ on Stack Overflow See other posts from Stack Overflow or by Champ
Published on 2012-12-06T16:54:58Z Indexed on 2012/12/06 17:04 UTC
Read the original article Hit count: 279

Filed under:
|
|
|
|

I have a function which have a long execution time.

 public void updateCampaign()
    {

        context.Session[processId] = "0|Fetching Lead360 Campaign";
        Lead360 objLead360 = new Lead360();
        string campaignXML = objLead360.getCampaigns();

        string todayDate = DateTime.Now.ToString("dd-MMMM-yyyy");
        context.Session[processId] = "1|Creating File  for Lead360 Campaign on " + todayDate;
        string fileName = HttpContext.Current.Server.MapPath("campaigns") + todayDate + ".xml";
        objLead360.createFile(fileName, campaignXML);

        context.Session[processId] = "2|Reading The latest Lead360 Campaign";
        string file = File.ReadAllText(fileName);

        context.Session[processId] = "3|Updating Lead360 Campaign";
        string updateStatus = objLead360.updateCampaign(fileName);
        string[] statusArr = updateStatus.Split('|');
        context.Session[processId] = "99|" + statusArr[0] + " New Inserted , " + statusArr[1] + " Updated , With " + statusArr[2] + " Error , ";
    }

So to track the Progress of the function I wrote a another function

public void getProgress()
    {
        if (context.Session[processId] == null)
        {
            string json = "{\"error\":true}";
            Response.Write(json);
            Response.End();
        }else{
            string[] status = context.Session[processId].ToString().Split('|');
            if (status[0] == "99") context.Session.Remove(processId);

            string json = "{\"error\":false,\"statuscode\":" + status[0] + ",\"statusmsz\":\"" + status[1] + "\" }";
            Response.Write(json);
            Response.End();  
        }
    }

To call this by jQuery post request is used

reqUrl = "AjaxPages/lead360Campaign.aspx?processid=" + progressID + "&action=updatecampaign";
$.post(reqUrl);
setTimeout(getProgress, 500);

get getProgress is :

function getProgress() {
    reqUrl = "AjaxPages/lead360Campaign.aspx?processid=" + progressID + "&action=getProgress";
    $.post(reqUrl, function (response) {
        var progress = jQuery.parseJSON(response);
        console.log(progress)
        if (progress.error) {
            $("#fetchedCampaign .waitingMsz").html("Some error occured. Please try again later.");
            $("#fetchedCampaign .waitingMsz").css({ "background": "url(common/images/ajax_error.jpg) no-repeat center 6px" });
            return;
        }
        if (progress.statuscode == 99) {
            $("#fetchedCampaign .waitingMsz").html("Update Status :"+ progress.statusmsz );
            $("#fetchedCampaign .waitingMsz").css({ "background": "url(common/images/ajax_loded.jpg) no-repeat center 6px" });
            return;
        }
        $("#fetchedCampaign .waitingMsz").html("Please Wait... " + progress.statusmsz);
        setTimeout(getProgress, 500);
    });
}

But the problem is that I can't see the intermediate message. Only the last message is been displayed after a long lime of ajax loading message

Also on the browser console I just see that after a long time first requested is completed and after that the second request is completed. but there should be for getProgress ?

I have checked jquery.doc and it says that $post is an asynchronous request.

Can anyone please explain what is wrong with the code or logic?

© Stack Overflow or respective owner

Related posts about c#

Related posts about jQuery