2
SQLサーバーからデータを読み取り、このWebページの後に[WebMethod]を使用して表示しようとしています。tutorial。私は自分のデータとロジックを使用しています。AJAXコールの空白出力
目標 - ボタンをクリックすると、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][email protected] group by [Incident Product Name];
END
ボタンをクリックすると出力が表示されませんを取得してください。コンソールでもチェックしましたが、エラーはありません。
デバッグ方法や問題へのアプローチ方法がわかりません。
EDITは - 2015年
onSuccessメソッドにconsole.logを入れましたか?それは何かを記録していますか?あなたのwebmethodにブレークポイントを置くと、あなたがデバッグするときにincidentListがヒットするのを返しますか? – Massanu
はい実際の機能を実装する前に 'function OnSuccess(response){ console.log(response.d) } 'を使用しました。まだ出力はありません。 –
@Massanuブレークポイントを使用しようとしましたが、ヒット数は0です。 –