I am new to jquery ,I am apparently trying to call a c-sharp function from javascript using ajax and jquery ,and I also want to pass some parameters while calling the c-sharp function ..
This is how I trying to do the same:
var _data = {
'_mStart': document.getElementById("St_Period"),
'_mEnd': document.getElementById("En_Period")
};
$.ajax({
type: "POST",
url: "maps.aspx/myFunc",
data: _data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("success!")
}
});
And here is my webmethod in aspx.cs
[WebMethod]
public static void myFunc(DateTime? _mStart, DateTime? _mEnd)
{
try
{
//string st = St_Period.Value.ToString();
//string end = En_Period.Value.ToString();
SqlConnection con=new SqlConnection("server=SWAPPS_LAP\\SQLEXPRESS;Initial Catalog=moogle;Integrated Security=True;MultipleActiveresultSets=true");
SqlCommand cmd = new SqlCommand();
con.Open();
cmd = new SqlCommand("insert into MEDIA_BOOKING(ST_PERIOD,END_PERIOD,ENTERED_BY,ENTERED_ON) values(@st,@end,@by,@on)", con);
cmd.Parameters.AddWithValue("@st", _mStart);
cmd.Parameters.AddWithValue("@end", _mEnd);
//cmd.Parameters.AddWithValue("@by", Session["login"].ToString());
cmd.Parameters.AddWithValue("@on", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
When I run the code I get an error like:
Uncaught TypeError: Illegal invocation jquery.js:7601
jQuery.extend.param.add jquery.js:7601
buildParams jquery.js:7658
buildParams jquery.js:7653
buildParams jquery.js:7653
jQuery.extend.param jquery.js:7621
jQuery.extend.ajax jquery.js:7467
savebook maps.aspx:398
onclick
You need to pass the elements values not, the DOM elements.
Instead of: