我正在尝试从 SQL Server 数据库中检索数据,并使用 jQuery ajax 来完成。我需要将这些数据带入一些 jqPlot 图表中,并认为这是最好的方法。如果有更好的方法,请告诉我。
这是我当前的代码:
Javascript/jQuery:
$(document).ready(function () {
$(".datepicker").datepicker();
$(".submitButton").click(handleSubmitButton);
}); //end of document ready
function handleSubmitButton() {
var AnDParms = new Object();
AnDParms.startDate = $('.start-date').val();
AnDParms.endDate = $('.end-date').val();
AnDParms.facility = $('.FacilityDDL').val();
var DTO = { 'parameters': AndParms };
$.ajax({
type: "GET",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(DTO),
url: "GetAdmitsDischarges.asmx.cs/GetAandD",
dataType: "json",
success: function (data) {
alert("Data: " + data.d);
// do chart stuff here.
},
error: function (data) {
alert("Error!" + data.d);
} //end of success
}); //end of ajax call
} //end of handleSubmitButton function
网络服务 ( GetAdmitsDischarges.asmx.cs
):
namespace DashboardTest2010
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class GetAdmitsDischarges : WebService
{
public class AnDParms
{
public string startDate { get; set; }
public string endDate { get; set; }
public string facility { get; set; }
}
public class AnDReturns
{
public string Admits { get; set; }
public string Discharges { get; set; }
}
[WebMethod]
public string GetAandD(AnDParms parameters)
{
//StringBuilder results = new StringBuilder();
AnDReturns resultsObj = new AnDReturns();
resultsObj.Admits = "0";
resultsObj.Discharges = "0";
string results = "";
string connect = (the data connection string)
StringBuilder query = new StringBuilder();
query.Append("Select 'Admissions' SeriesType,");
//the rest of the query.
using (SqlConnection conn = new SqlConnection(connect))
{
using (SqlCommand cmd = new SqlCommand(query.ToString(), conn))
{
cmd.Parameters.AddWithValue("StartDate", parameters.startDate.ToString());
cmd.Parameters.AddWithValue("EndDate", parameters.endDate.ToString());
cmd.Parameters.AddWithValue("FacilityID", parameters.facility.ToString());
SqlDataReader rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
while (rdr.Read())
{
if (rdr["SeriesType"].ToString() == "Admissions")
{
resultsObj.Admits = rdr["SeriesCount"].ToString();
}
if (rdr["SeriesType"].ToString() == "Discharges")
{
resultsObj.Discharges = rdr["SeriesCount"].ToString();
}
}
results = JsonConvert.SerializeObject(resultsObj);
}
}
}
return results;
}
}
}
标记只是一个简单的页面,其中包含 2 个日期字段(设置为 datepickers)、1 个下拉列表(设施)和一个提交按钮。这是我的脚本标签,以确保我拥有所需的一切:
<script type="text/javascript" src="js/jqPlot/jquery.jqplot.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/jqPlot/jquery.jqplot.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.3.custom.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/DashboardTest.js"></script>
“jquery.js”是 jqPlot 附带的 jquery。我必须支持 IE 7+(因此是 json2.js) DashboardTest.js 是上面的 javascript。
当我运行它时没有任何反应,当我单击提交按钮时没有显示任何警报。任何帮助将非常感激。我的系统管理员不允许我安装 Fiddle,如果您对此感到疑惑...