Peter Olson
Peter Olson

Reputation: 142921

Multiple Response AJAX request

Is there a way to have one AJAX request with multiple responses?

For example, if make a GET request to the server which will take a long time to calculate, how could I have the server occasionally send back responses which give me some data about the progress?

If so, could somebody post an example, preferably with Jquery and an explanation of the mechanism through which the server can does it?

Upvotes: 33

Views: 32338

Answers (5)

The Scrum Meister
The Scrum Meister

Reputation: 30111

You can implement this using 2 ajax calls, one to run the process and a second call to periodically poll the progress:

On the server side:

public class ProgressInfo
{
    public int Percent {get;set;}
    public bool Done {get;set;}
}

public JsonResult DoCalculation(string id)
{
    ProgressInfo progress = new ProgressInfo();
    if(!string.IsNullOrEmpty(id))
    {
        Session[id] = progress;
    }

    //periodicly update progress
    progress.Percent++;
}

public JsonResult GetProgress(string id)
{
    ProgressInfo progress;
    if(string.IsNullOrEmpty(id)
        || (progress = Session[id] as ProgressInfo) == null)
    {
        return Json(new {
            success = false
        });
    }
    if(progress.done)
    {
        Session.Remove(id);
    }
    return Json(new {
        success = true,
        done = progress.done,
        percent = progress.Percent
    });
}

On the client side:

var progressID = Math.random();

function doCalculation() {
    $.post('<%=Url.Action("DoCalcluation")%>/' + progressID);
    setTimeout(pollProgress, 1000);
}

function pollProgress() {
    $.post('<%=Url.Action("GetProgress")%>/' + progressID, function(response){
        if(!response.success) {
            alert('Cannot find progress');
            return;
        }
            if(response.done) {
                alert('Done!');
            } else {
            alert('Progress at ' + response.precent + '%');
            setTimeout(pollProgress, 1000 /*1 second*/);
            }
    }, 'json');
}

Upvotes: 28

Nirbhay Mishra
Nirbhay Mishra

Reputation: 1648

One Ajax request ----------->You get One response.

What you can actually do is.

check the response data and do DIFFERENT action as per the data fetched.

Upvotes: 0

Slakkie
Slakkie

Reputation: 11

Have a look at xajax it can send multiple responses back, and one can ever redirect them to certain areas. In this regard xajax is much better than jQuery. I can make one request and the server can compose a response which contains multiple responses either HTML, JavaScript, calls, etc. You can even say put this there and that there with options like append, replace etc. very cool. jQuery should do this then I can switch once and for all to jQuery... This is the only thing that is keeping me from switching over.

Upvotes: 1

vlscanner
vlscanner

Reputation: 458

You most likely need help from the server side coding because looks like you need reverse ajax or what is also called comet push. I do not know which language you use on server side but the basic idea is to delay http response for as long as browser will allow using endless loop (on server side) and push data while connection is alive

You may want to check this out: http://code.google.com/p/google-web-toolkit-incubator/wiki/ServerPushFAQ

Upvotes: 1

genesis
genesis

Reputation: 50976

Fast answer: No, it isn't possible.

You should send more requests to get more responses

Upvotes: 6

Related Questions