gpt4 book ai didi

javascript - 无法将参数传递给谷歌饼图MySql查询

转载 作者:行者123 更新时间:2023-11-29 16:50:11 25 4
gpt4 key购买 nike

我有这个 asp 标签,它从 session 中获取其值。我想将它作为参数传递给 WebMethod 中的 MySql 查询,该查询用于此处的 google 饼图。我尝试了很多,每次标签在 webmethod 中都返回 null。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var options = {
title: 'Shift based data',
legend: { position: 'right', textStyle: { color: 'blue', fontSize: 16 } }
};
$.ajax({
type: "POST",
url: "grpm.aspx/GetChartData",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",

success: function (r) {
var data = google.visualization.arrayToDataTable(r.d);
var chart = new google.visualization.PieChart($("#chart")[0]);
chart.draw(data, options);
},
failure: function (r) {
alert(r.d);
},
error: function (r) {
alert(r.d);
}
});
}
</script>
<asp:Label ID="uName" runat="server"></asp:Label>

下面的静态方法

[System.Web.Services.WebMethod]
public static List<object> GetChartData()
{
string query = "SELECT tm AS TM, COUNT(agentlogin)AS totalApproved, shift AS Shift FROM approved WHERE grpM = @grpm GROUP BY shift";
string MyConString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
List<object> chartData = new List<object>();
chartData.Add(new object[]
{
"Shift", "totalApproved"
});
using (MySqlConnection con = new MySqlConnection(MyConString))
{
using (MySqlCommand cmd = new MySqlCommand(query))
{

cmd.Connection = con;
Page page = (Page)HttpContext.Current.Handler as Page;
Label login = (Label)page.FindControl("uName") as Label;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@grpm", login);
con.Open();
using (MySqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
chartData.Add(new object[]
{
sdr["Shift"], sdr["totalApproved"]
});
}
}
con.Close();
return chartData;
}
}
}

我听说有一种方法可以通过 jquery 获取控件。如果有我如何将其传递给 WebMethod 中的查询。我怎样才能完成这件事。提前致谢。

最佳答案

1) 将 string 参数添加到您的 WebMethod 中,例如

[WebMethod]
public static List<object> GetChartData(string name)
{
//Your stuff here
}

2) 并从 js 向上述方法发送 string 参数,例如

var obj = {};
obj.name = 'Your value here';

$.ajax({
type: "POST",
url: "grpm.aspx/GetChartData",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",

//Your stuff here
});

关于javascript - 无法将参数传递给谷歌饼图MySql查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52853108/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com