A pending ajax request gets dismissed and return value is never retrieved after sending another

I have a jQuery ajax post request that starts when the page loads, and the php that the request contacts to will loop until some event occurs. After this event occurs, the php will print the result and the ajax will return this value.

This works perfectly until another ajax post request is sent by the user of the page. The first request will not return any value until after the pending request starts again and gets something.

The fault is likely not in the PHP since sometimes I do get a result from the first ajax request.

My current thought is that somehow the new ajax request overrides the previous, pending request.

How should I go about fixing this?

Example code:

This is fired with $(document).ready:

function requestMsg() 
{   
    getMsg = $.post("getMessage.php", function(e)
    {
        var data = e.split(":");

        switch(data[0])
        {
            case "msg":                 
                function1(data[1], data[2]);
                break;
            default:
                function2(data[1], data[0]);
                break;
        }
        requestMsg();
    });
}
requestMsg();

This is the other request that the user fires:

$.post("SendMessage.php", {"to": recipient, "msg": text});


Source: ajax

Leave a Reply