[英]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.