按钮onclientclick调用ajax

时间:2014-05-10 04:50:18

标签: asp.net ajax web-services

在我的asp.net应用程序中,我有一个按钮定义为:

Asp.net

<asp:Button ID="TrackHistoryButton" runat="server" Text="Get Sensor History" CssClass="btn btn-default"  ValidationGroup="HistoryDateValidationGroup"   OnClick="TrackHistoryButton_Click" OnClientClick="GenerateHistoryChart()" UseSubmitBehavior="false"   />

的JavaScript

 function GenerateHistoryChart()
    {
    if ($("#HistoryDisplayType").val() === "Graph")
    {
        var sensorId = $("#SensorIdListBox option:selected").val();
        var startDate = $("#StartHistoryDate").val();
        var endDate = $("#EndHistoryDate").val();
        $.ajax({
            type: "POST",
            url: "Options.aspx/GetDataForChart",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: '{ sensorId:"'+ sensorId +'", startDate:" '+startDate +'", endDate: "'+ endDate +'"}',                 
            success: OnSuccess,
            error: function (response) {
                alert(response.status);
            }
        });
       // return false;
    }
    else
        return true;
    }

function OnSuccess(data)
{
    chart = new Highcharts.StockChart(levelChartData);

    var historyData1 = [],
        historyData2 = [],
        series1 = chart.series[0], //level data
        series2 = chart.series[1]; //freshness timer
    $.each(data.d, function () {
        x = (new Date(Date.parse(this.SensorDataInfo.ServerTime))).getTime(), // current time
       y = this.levelData.y,
       z = this.levelData.z;

        historyData1.push([x, y]);
        historyData2.push([x, z]);
    })
    series1.setData(historyData1);
    series2.setData(historyData2);
}

C#CODE

[System.Web.Services.WebMethod]
    public static List<object> GetDataForChart(string sensorId, string startDate, string endDate)         
    {
       List<SD> result = SensorMapper.Instance.GetSensorHistory(Int64.Parse(sensorId), DateTime.Parse(startDate), DateTime.Parse(endDate));         
        List<object> result2 = new List<object>();
        result2.AddRange(result.Select(r => new object[] { r.SensorDataInfo.ServerTime, r.temperatureData.temp }));
        return result2;
    }

一切正常但OnSuccess功能未被触发。我在jsonlint.com上检查了我的JSON数据格式并对其进行了验证。有人可以帮我找到问题吗?

1 个答案:

答案 0 :(得分:0)

试试这个....在传递参数时删除单引号..

 $.ajax({
                type: "POST",
                url: "Options.aspx/GetDataForChart",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data:{ sensorId: sensorId, startDate: startDate, endDate: endDate},
                success: function (data){chart = new Highcharts.StockChart(levelChartData);

            var historyData1 = [],
                historyData2 = [],
                series1 = chart.series[0], //level data
                series2 = chart.series[1]; //freshness timer
            $.each(data.d, function () {
                x = (new Date(Date.parse(this.SensorDataInfo.ServerTime))).getTime(), // current time
               y = this.levelData.y,
               z = this.levelData.z;

                historyData1.push([x, y]);
                historyData2.push([x, z]);
            })
            series1.setData(historyData1);
            series2.setData(historyData2);
                },
                error: function (response) {
                    alert(response.status);
                }
            });

如果它不起作用,请告诉我......