I know we already have many posts about this topic, but I just cannot figure out how to pass the data to WebMethod! All I need is to pass one string data to my WebMethod, but do I need to use list data type?
.js
function buttonClicked () {
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "Chat.aspx/send",
data: "{'text':'" + $("#writeBox").val() + "'}",
success: function () {
},
error: function () {
}
});
}
.aspx (updated with full codes)// I commented out the part that incurs error
[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();
}
}
1 Answer