简体   繁体   中英

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

I am writing a webpage which displays many different charts, using AJAX to get the data from the database and google charts to display the information.

Is there a way that I can pass in the stored procedure name from AJAX to the code behind webMetod? At the moment I am having to create a separate webMethod for each stored procedure whereas I would like to just have one method where I pass which procedure to call.

I have tried adding as a parameter and data but have not been able to make it work.

I am using WCF service to pull data from the database, here is my WebMethod, you will see the stored proc name 'GetChartDataConceptDoubleLayerComboTwoBars' which I would like to pass as a param.

[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;
}

Here is the Ajax

$.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);
    }
});

you can add "stored procedure name" as query parameter to the ajax url property.

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

And access it in the web method as shown below

string storedProcName = HttpContext.Current.Request.QueryString["storedProcName"];

Write a switch statement with storedProcName as the parameter and write a case statement for each stored proc. Using this approach you can avoid creating a separate web method for each stored proc and everything can be handled in single web method.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM