Ajax异步调用Asp.net没有提供正确的输出

时间:2016-01-14 22:14:34

标签: jquery asp.net ajax

我有两个ajax调用,我希望这些调用是异步的

        var toDate = '<%=ucChartDateSelector1.ToDate%>';
        var fromdate = '<%=ucChartDateSelector1.FromDate%>';
        var reportFor = '<%=ucChartDateSelector1.ddlReportType.SelectedItem.Text%>';
        var periodFor = '<%=ucChartDateSelector1.ddlDateSelection.SelectedItem.Text%>';

        $(document).ready(function () {
               GenerateChartReport();
        });

        function GenerateChartReport() {
            $.ajax({
                type: 'POST',
                url: 'Dashboard.aspx/ReadBooking',
                contentType: 'application/json',
                dataType: 'json',
                async: true,
                data: JSON.stringify({ fromDate: fromdate, toDate: toDate, reportFor: reportFor, periodFor: periodFor }),
                success: function (response) {
                    TwoColumnReport(response.d, "chartLine_div", "Google Dealy Example");

                },
                error: function (error) {
                    console.log(error);
                }

            }),
            $.ajax({
                type: 'POST',
                url: 'Dashboard.aspx/GetData', 
                contentType: 'application/json',
                dataType: 'JSON',
                async: true,
                success: function (response) {
                    TwoColumnReport(response.d, "visualization", "Google Charts Example");
                },
                error: function (error) {
                    console.log(error);
                }

            });

            return false;
        }
        function TwoColumnReport(dataValues, mainDivId, reportTitle) {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Column Name');
            data.addColumn('number', 'Column Value');

            for (var i = 0; i < dataValues.length; i++) {
                data.addRow([dataValues[i].ColumnName, dataValues[i].Value]);
            }
            new google.visualization.PieChart(document.getElementById(mainDivId)).
               draw(data, { title: reportTitle });
        }

在服务器端,我有以下代码。一个功能延迟2秒,另一个功能延迟5秒。

[WebMethod]
public static List<Data> GetData()
{
    logger.WriteInfoLog(String.Format("***********GetData Start**********    "));
    int milliseconds = 2000;
    Thread.Sleep(milliseconds);
    List<Data> dataList = new List<Data>();

    dataList.Add(new Data("Column 1", 100));
    dataList.Add(new Data("Column 2", 200));
    dataList.Add(new Data("Column 3", 300));
    dataList.Add(new Data("Column 4", 400));
    logger.WriteInfoLog(String.Format("*********GetData End**********"));
    return dataList;
}

[WebMethod]
public static List<Data> ReadBooking(String fromDate, String toDate, String reportFor, String periodFor)
{
    logger.WriteInfoLog(String.Format("---------ReadBooking Summary Start.   Report type {0}-{3}  DateRange {1}- {2}----- ", reportFor, fromDate, toDate, periodFor));
    int milliseconds = 5000;
    Thread.Sleep(milliseconds);
    List<Data> dataList = new List<Data>();

    dataList.Add(new Data("row 1", 1400));
    dataList.Add(new Data("row 2", 3200));
    dataList.Add(new Data("row 3", 3100));
    dataList.Add(new Data("row 4", 4100));
    dataList.Add(new Data("row 5", 2400));
    logger.WriteInfoLog(String.Format("---------ReadBooking Summary. End----- "));
    return dataList;
}

我希望日志按以下顺序排列:

 --------ReadBooking Summary Start.   Report type Weekly -11 Jan 2016 
   DateRange 11/01/2016 12:00:00 AM- 17/01/2016 12:00:00 AM-----  
   ***********GetData Start**********
        *********GetData End********** 
       ---------ReadBooking Summary. End-----

但我得到的输出是:

 ---------ReadBooking Summary Start.   Report type Weekly -11 Jan 2016  DateRange 11/01/2016 12:00:00 AM- 17/01/2016 12:00:00 AM----- 
---------ReadBooking Summary. End----- 
***********GetData Start**********
*********GetData End**********

我错过了什么?

1 个答案:

答案 0 :(得分:0)

在ASP.NET中,如果两个请求来自同一个会话,则它们可以独占访问SessionState,也就是说它们按顺序运行,而不是并行运行。

在你的例子中,它们都被同时触发,但是第二个到达的第二个被放入队列直到第一个完成,因为它们都来自Session(基于SessionId)。

您可以通过将以下属性应用于控制器来解决问题:

[SessionState(SessionStateBehavior.Disabled)]

当然,您无法访问HttpContext.Current.Session属性(嗯,您可以,但它会是null

更多参考资料: