c#从单个ajax调用获取进度更新

时间:2015-01-07 04:48:28

标签: javascript c# jquery asp.net ajax

我正在尝试进行AJAX(JQuery)调用,URL会定期将响应发送回浏览器,但我只是想知道如何捕获响应。

html js:

$.ajax({
            type: 'GET',
            datatype: 'json',
            url: Progess.ashx,
            success: function (data) {
                if (data.Working) {
                    $('#txt_test').val('Working on: ' + data.Current + '/' + data.Final);
                }
                else {
                    if (data.Success) {
                        alert('finished');
                    }
                }
            },                
            error: function () { alert('unable to call Progress.ashx and return valid JSON results'); }
        });

我的ASP.NET处理程序:

public void ProcessRequest (HttpContext context) {        
    for (int i = 0; i < 20; i++)
    {
        ProgressRespond(true, "", context, 0, 19, i, true);
        System.Threading.Thread.Sleep(1000);
    }
    ProgressRespond(true, "", context, 0, 19, 19, false);
}

public void ProgressRespond(bool success, string message, HttpContext context, int initial, int final, int current, bool working, object obj = null)
{
    ProgressClass response = new ProgressClass();
    response.Success = success;
    response.Message = message;
    response.Initial = initial;
    response.Final = final;
    response.Current = current;
    response.Working = working;
    response.Data = obj;
    string jsonString = JsonConvert.SerializeObject(response, new JsonSerializerSettings { DateTimeZoneHandling = DateTimeZoneHandling.Local });
    context.Response.ContentType = "application/json";
    context.Response.ContentEncoding = System.Text.Encoding.UTF8;
    context.Response.Clear();
    context.Response.Write(jsonString);
    if (success)
        context.Response.End();
    else
        context.Response.Flush();
}

private class ProgressClass
{
    public int Initial { set; get; }
    public int Final { set; get; }
    public int Current { set; get; }
    public bool Working { set; get; }
    public bool Success { set; get; }
    public string Message { set; get; }
    public object Data { set; get; }
}

我希望在AJAX函数中的某个地方我可以捕获每个响应并利用在最终响应之前返回的数据。 “成功”回调似乎只是从处理程序接收到Response.End()时才触发,但两者之间没有任何内容。

我基本上是尝试使用JQuery AJAX实现Comet。这可能吗?

感谢。

2 个答案:

答案 0 :(得分:0)

我猜你正试图创造一个像现代的#34; socket&#34;。我找到了一个可能有帮助的类似post related to Comet, Ajax, and Sockets

答案 1 :(得分:0)

只是大声思考,你需要的是一个jquery promise对象,它通知进行状态的ajax调用。以下是正在进行的API:

根据jquery deferred.progress(progressCallbacks)做:&#34;添加要在Deferred对象生成进度通知时调用的处理程序。&#34;有些人做了一些小提琴,以显示它是如何工作的,你可以在这里找到jsFiddle http://jsfiddle.net/hodgepodge/Xv8nD/

如果链接死亡,他的jsFiddle的内容就在这里:

var countUpTimer;
var countUp_number = -10;
var deferred = $.Deferred();
var promise = deferred.promise();

$(document).ready(function () {
    $("#progressbar").progressbar();
    $("#progressbar").css({
        'background': 'LightYellow'
    });
    $("#progressbar > div").css({
        'background': 'Orange'
    });

    $("#fail").click(function () {
        deferred.reject();
    });

    function result() {
        alert("Done!");
    }

    function failed() {
        $("#progressbar").css({
            'background': 'red'
        });
    }

    function inProgress() {
        $("#progressbar").progressbar({
            value: countUp_number
        });
        $("#progressbar > span").html(countUp_number + "%");
    }

    function countUp() {
        if (countUp_number < 100) {
            countUp_number += 10;
            deferred.notify();
            countUpTimer = setTimeout(countUp, 500);
        } else {
            deferred.resolve();
        }
    }

    promise.done(result);
    promise.fail(failed);
    promise.progress(inProgress);

    countUp();
});

****************的Html **********

<div id="progressbar" style="margin: 0px 0px 16px 0px;"><span style="position: absolute;text-align: center;margin: 5px 0 0 45%;"></span>