簡體   English   中英

WCF服務-使用AJAX傳遞存儲過程名稱作為數據

[英]WCF Service - using AJAX to pass stored procedure name as data

我正在寫一個顯示許多不同圖表的網頁,使用AJAX從數據庫中獲取數據,並使用Google圖表顯示信息。

有什么方法可以將存儲過程名稱從AJAX傳遞給webMetod背后的代碼? 目前,我必須為每個存儲過程創建一個單獨的webMethod,而我只想有一個方法可以傳遞要調用的過程。

我嘗試添加作為參數和數據,但無法使其工作。

我正在使用WCF服務從數據庫中提取數據,這是我的WebMethod,您將看到存儲的過程名稱“ GetChartDataConceptDoubleLayerComboTwoBars”,我希望將其作為參數傳遞。

[WebMethod]
public static List<ChartData> GetDoubleLayerChartTwoBars()
{
    System.ServiceModel.BasicHttpBinding userHttpBinding = new System.ServiceModel.BasicHttpBinding();
    userHttpBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue;
    userHttpBinding.MaxReceivedMessageSize = int.MaxValue;
    System.ServiceModel.EndpointAddress userHtpEndpointAddress = new System.ServiceModel.EndpointAddress(chartDataDoubleLayerFactoryURI);
    ChartDataDoubleLayerTDFactory chartFactory = new ChartDataDoubleLayerTDFactory(userHttpBinding, userHtpEndpointAddress);

    TDBindingList<ChartDataDoubleLayerTD> chartDataRaw = chartFactory.GetChartDataConceptDoubleLayerComboTwoBars();

    List<ChartData> chartData = new List<ChartData>();
    chartData = channelReportingSummary.getData(chartDataRaw);

    chartFactory.Close();

    return chartData;
}

這是阿賈克斯

$.ajax({
    type: 'POST',
    contentType: 'application/json; charset=utf-8',
    processData: false,
    url: 'channelReportingSummary.aspx/GetDoubleLayerChartTwoBars',
    dataType: 'json',
    timeout: 120000,
    async: false,
    success: function(result) {
        createDatatableCombo(result, data, options);
    },
    error: function(xhr) {
        alert(xhr.responseText);
    }
});

您可以將“存儲過程名稱”作為查詢參數添加到ajax url屬性中。

url: 'channelReportingSummary.aspx/GetDoubleLayerChartTwoBars?storedProcName=GetChartDataConceptDoubleLayerComboTwoBars'

並通過如下所示的web方法訪問它

字符串storedProcName = HttpContext.Current.Request.QueryString [“ storedProcName”];

編寫一個以storedProcName作為參數的switch語句,並為每個存儲的proc編寫一個case語句。 使用這種方法,您可以避免為每個存儲的proc創建單獨的Web方法,並且所有事情都可以在單個Web方法中處理。

switch (storedProcName)
    {
        case "GetChartDataConceptDoubleLayerComboTwoBars":
            {
                TDBindingList<ChartDataDoubleLayerTD> chartDataRaw = chartFactory.GetChartDataConceptDoubleLayerComboTwoBars();
                break;
            }
        default:
            break;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM