[英]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. 我正在写一个显示许多不同图表的网页,使用AJAX从数据库中获取数据,并使用Google图表显示信息。
Is there a way that I can pass in the stored procedure name from AJAX to the code behind webMetod? 有什么方法可以将存储过程名称从AJAX传递给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.
目前,我必须为每个存储过程创建一个单独的webMethod,而我只想有一个方法可以传递要调用的过程。
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. 我正在使用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;
}
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. 您可以将“存储过程名称”作为查询参数添加到ajax url属性中。
url: 'channelReportingSummary.aspx/GetDoubleLayerChartTwoBars?storedProcName=GetChartDataConceptDoubleLayerComboTwoBars'
And access it in the web method as shown below 并通过如下所示的web方法访问它
string storedProcName = HttpContext.Current.Request.QueryString["storedProcName"]; 字符串storedProcName = HttpContext.Current.Request.QueryString [“ storedProcName”];
Write a switch statement with storedProcName as the parameter and write a case statement for each stored proc. 编写一个以storedProcName作为参数的switch语句,并为每个存储的proc编写一个case语句。 Using this approach you can avoid creating a separate web method for each stored proc and everything can be handled in single web method.
使用这种方法,您可以避免为每个存储的proc创建单独的Web方法,并且所有事情都可以在单个Web方法中处理。
switch (storedProcName)
{
case "GetChartDataConceptDoubleLayerComboTwoBars":
{
TDBindingList<ChartDataDoubleLayerTD> chartDataRaw = chartFactory.GetChartDataConceptDoubleLayerComboTwoBars();
break;
}
default:
break;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.