gpt4 book ai didi

jquery - 将 jquery 数据传递给 WebMethod

转载 作者:行者123 更新时间:2023-12-01 01:27:44 26 4
gpt4 key购买 nike

我知道我们已经有很多关于这个主题的帖子,但我就是不知道如何将数据传递给 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/

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