Ajax server side setup and teardown

时间:2017-07-10 15:24:24

标签: javascript ajax

I have the following situation:

Server-side, I have a relational database, and am doing some rather computationally-intensive requests on it. For a request, there is a resource intensive setup task. Many of my requests require this same setup step, which I don't want to repeat, which leads me to want a single call. However, each request takes a lot of time, which makes me want to issue them asynchronously as several calls, so that the user gets stuff as it becomes available. I could try to just "keep the setup step around", but I don't really want the server to try to guess when the client is done, and it can't rely on the client to tell it when to clean up.

Here's what I would LIKE to happen: - Client-side, I gather up the requests that I want to make, A,B and C. They all require the same setup step. - So that I don't have to repeat the setup step, I issue one ajax call for A,B and C simultaneously. - So that the user doesn't have to wait forever for results, I return answers for A,B and C asynchronously, handing them back to the client as the results become available.

Something like:

$.ajax({
    type: "GET",
    url: "/?A=1&B=2&C=3",
    partialSuccess: function (data) {
        if (partialSuccess.which == "A") {
            doStuffForTaskA();
        }
    },
    success: function (data) {
        console.log("all tasks complete!");
    }
});

I'm pretty sure the sort of thing I have in the code above is not possible. Any thoughts on the best way to accomplish what I'm trying to do? (I am also the author of the server-side code. It happens to be c sharp, but I'm somewhat more interested in this as a 'which protocol, and how does it work' question)

0 个答案:

没有答案