我知道我们已经有很多关于这个主题的帖子,但我就是不知道如何将数据传递给 WebMethod!我所需要的只是将一个字符串数据传递给我的 WebMethod,但是我需要使用列表数据类型吗?
.js
function buttonClicked () {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "Chat.aspx/send",
data: "{'text':'" + $("#writeBox").val() + "'}",
success: function () {
},
error: function () {
}
});
}
.aspx(已更新完整代码)//我注释掉了发生错误的部分
[System.Web.Services.WebMethod]
void send (string text) {
string id = HttpContext.Current.Request.Cookies["id"].ToString();
string queryString = "INSERT INTO Log (ID, Text, Date) Values (" + id + ", " + text + ", GETDATE())";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=ChatV1;Integrated Security=True");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(queryString, con);
using (con)
{
con.Open();
//cmd.ExecuteNonQuery();
}
}
最佳答案
[System.Web.Services.WebMethod]
public static void send (string text) {
string id = HttpContext.Current.Request.Cookies["id"].ToString();
string queryString = "INSERT INTO Log (ID, Text, Date) Values (" + id + ", " + text + ", GETDATE())";
System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=ChatV1;Integrated Security=True");
System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(queryString, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
关于jquery - 将 jquery 数据传递给 WebMethod,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8768432/