我正在尝试从SQL服务器读取数据并使用[{3}}之后的[WebMethod]在网页上显示。我正在使用自己的数据和逻辑。
目标 - 单击按钮时显示基于ProductName的事件计数。
WebService.cs
public class Incidents
{
public string incidentProductName;
public string incidentCount;
public string incidentProductSearch;
}
[WebMethod]
public List<Incidents> getCountOfIncidents(List<string> aData)
{
SqlDataReader dr;
List<Incidents> incidentList = new List<Incidents>();
string conn = "Data Source = (LocalDb)\\MSSQLLocalDB; Initial Catalog = test; Integrated Security = True";
using (SqlConnection con = new SqlConnection(conn))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "spGetIncidents";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection = con;
cmd.Parameters.AddWithValue("@ProductSearch", aData[0]);
con.Open();
dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
if (dr.HasRows)
{
while (dr.Read())
{
string incidentproductname = dr["incidentProductName"].ToString();
string incidentcount = dr["incidentCount"].ToString();
string ProductSearch = dr["incidentProductSearch"].ToString();
incidentList.Add(new Incidents
{
incidentProductName = incidentproductname,
incidentCount = incidentcount,
incidentProductSearch = ProductSearch
});
}
}
return incidentList;
}
}
}
Default.aspx的
<form id="form1" runat="server">
<div>
<script>
$("#myButton").on("click", function (e) {
e.preventDefault();
var aData = [];
aData[0] = $("#ddlSelectProductName").val();
$("#contentHolder").empty();
var jsonData = JSON.stringify({ aData: aData });
$.ajax({
type: "POST",
//getCountOfIncidents is my webmethod
url: "WebService.asmx/getCountOfIncidents",
data: jsonData,
contentType: "application/json; charset=utf-8",
dataType: "json", // dataType is json format
success: OnSuccess,
error: OnErrorCall
});
function OnSuccess(response) {
var items = response.d;
var fragment = "<ul>"
$.each(items, function (index, val) {
var incidentProductName = val.incidentProductName;
var incidentCount = val.incidentCount;
var incidentProductSearch = val.incidentProductSearch;
fragment += "<li> " + incidentProductName + " :: " + incidentCount + " - " + incidentProductSearch + "</li>";
});
$("#contentHolder").append(fragment);
}
function OnErrorCall(response) { console.log(error); }
});
</script>
<select id="ddlSelectProductName">
<option>ORACLE</option>
<option>TERADATA</option>
</select>
<button id="myButton">Get List</button>
<div id="contentHolder"></div>
</div>
</form>
SQL PROC
CREATE PROCEDURE spGetIncidents(
@ProductSearch varchar(20)
)
AS
BEGIN
select [Incident Product Name],count([Incident Product Name]) as count from incidents where [Incident Product Name]=@ProductSearch group by [Incident Product Name];
END
代码执行但是当我点击按钮获取列表时,我看不到输出。我也检查了Console,没有错误。
我不确定如何调试或解决问题。
编辑 - 当我尝试使用断点时,我的Webmethod有0次点击次数
其他详细信息 -
答案 0 :(得分:1)
我要看的两件事:
cache: false
。您可能没有返回任何数据,并且已经缓存,因此它不再适用于您的webmethod。<system.webServer>
<handlers>
<remove name="WebServiceHandlerFactory-Integrated"/>
<remove name="ScriptHandlerFactory"/>
<remove name="ScriptHandlerFactoryAppServices"/>
<remove name="ScriptResource"/>
<add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
<add name="ScriptResource" verb="GET,HEAD" path="ScriptResource.axd" preCondition="integratedMode" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</handlers>
</system.webServer>
它更多地与AJAX工具包有关,但如果没有它们,这些项目就不起作用。