我创建了一个 Web 服务,其中有一个函数可以计算我的 SQL 数据库中的一些数据。这是我的 WebService.asmx 的代码:
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public int SalesNumberMonth(int i)
{
int total = 0;
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Sql"].ConnectionString);
try
{
string request = "SELECT * FROM sales_Ventes V INNER JOIN sys_AnneesFiscales A ON V.AnneeFiscale = A.Code INNER JOIN sys_Mois M ON V.Mois = M.Code WHERE M.Code='" + i + "'" + " AND Active = 'true'";
connection.Open();
SqlCommand Req = new SqlCommand(request, connection);
SqlDataReader Reader = Req.ExecuteReader();
while (Reader.Read())
{
total++;
}
Reader.Close();
}
catch
{
}
connection.Close();
return total;
}
}
这里是我的 script.js :
var sin = [], cos = [];
for (var i = 1; i < 13; i += 1) {
GestionPro.WebService1.SalesNumberMonth(i, function (e) { sin.push([i, e]); } ,function (response) { alert(response); } );
cos.push([i, 2]);
}
var plot = $.plot($("#mws-test-chart"),
[{ data: sin, label: "Sin(x)²", color: "#eeeeee" }, { data: cos, label: "Cos(x)", color: "#c5d52b"}], {
series: {
lines: { show: true },
points: { show: true }
},
grid: { hoverable: true, clickable: true }
});
我的问题在这条线上:
GestionPro.WebService1.SalesNumberMonth(i, function (e) { sin.push([i, e]); } ,function (response) { alert(response); } );
当我交换这两个函数时,警报显示得很好,但是按这个顺序我不能在 sin[] 中添加我的函数的值。我应该错过一些东西,但不知道是什么......