我是 jquery 的新手,我显然正在尝试使用 ajax 和 jquery 从 javascript 调用 c-sharp 函数,并且我还想在调用 c-sharp 函数时传递一些参数..这就是我尝试做同样的事情:
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!")
}
});
这是我在 aspx.cs 中的 webmethod
[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;
}
}
当我运行代码时,出现如下错误:
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