Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing jquery data to WebMethod

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();
    }
}
like image 308
Si Young Kim Avatar asked Aug 27 '26 02:08

Si Young Kim


1 Answers

[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();
}
like image 64
Ali Foroughi Avatar answered Aug 29 '26 14:08

Ali Foroughi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!