我在 javascript 中有一个代码,它在 c-sharp 中调用了一个 webmethod,下面给出了 javascript 代码和 web 方法
javascript代码:
var _data = {
'_mStart': document.getElementById("St_Period").value,
'_mEnd': document.getElementById("En_Period").value
};
$.ajax({
type: "POST",
url: "maps.aspx/myFunc",
data: _data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert("success!")
}
});
网页方式:
[WebMethod]
public static void myFunc(DateTime? _mStart, DateTime? _mEnd)
{
try
{
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("@on", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
但是当我运行代码时,我得到一个错误
POST http://localhost:50060/moogle/maps.aspx/myFunc 500 (Internal Server Error) jquery.js:8102
jQuery.ajaxTransport.send jquery.js:8102
jQuery.extend.ajax jquery.js:7580
savebook maps.aspx:398
onclick
我有一种预感,也许参数有问题,也许它们的格式不正确?