ajax请求ashx报错object XMLHttpRequest
function saveclick() {
var subject = $("#<%=subject.ClientID %>").attr("value");
var starttime = $("#<%=starttime.ClientID %>").attr("value");
var endtime = $("#<%=endtime.ClientID %>").attr("value");
var isallday = $("#<%=isallday.ClientID %>").attr("checked");
var warn = $("#<%=warn.ClientID %>").attr("checked");
var description = $("#<%=description.ClientID %>").val();
$.ajax({
type: "post",
// contentType: "application/json",
dataType: "json",
cache: false,
url: "MyLog.ashx?subject=" + subject + "&starttime=" + starttime + "&endtime=" + endtime + "&isallday=" + isallday + "&warn=" + warn + "&description=" + description,
success: function () {
alert("添加成功");
}
});
parent.$.ligerDialog.close();
parent.$(".l-dialog,.l-window-mask").remove();
}
<input id="Button2" type="button" value="确定" runat="server" class="button2" onclick="saveclick()"/>
public class MyLog : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string cnnstr = ConfigurationManager.ConnectionStrings["WorkDiaryConstr"].ConnectionString;
SqlConnection connection = new SqlConnection(cnnstr);
String insertcmd = "insert into WorkDiary(Work_UserID,Work_Title,Work_Description,Work_StartTime,Work_EndTime,Work_IsAllDayEvent,Work_WarnID,Work_Warn_State) values(newid(),@subject,@description,@starttime,@endtime,@isalldayevent,@warnid,@state)";
SqlCommand mycmd = new SqlCommand(insertcmd, connection);
mycmd.Parameters.AddWithValue("@subject", context.Request.Params["subject"].ToString());
mycmd.Parameters.AddWithValue("@description", context.Request.Params["description"].ToString());
mycmd.Parameters.AddWithValue("@starttime", Convert.ToDateTime(context.Request.Params["starttime"].ToString()));
mycmd.Parameters.AddWithValue("@endtime", Convert.ToDateTime(context.Request.Params["endtime"].ToString()));
if (context.Request.Params["isallday"].ToString() == "true")
{
mycmd.Parameters.AddWithValue("@isalldayevent", 1);
}
else
mycmd.Parameters.AddWithValue("@isalldayevent", 0);
if (context.Request.Params["warn"].ToString() == "true")
{
mycmd.Parameters.AddWithValue("@warnid", 1);
}
else
mycmd.Parameters.AddWithValue("@warnid", 0);
mycmd.Parameters.AddWithValue("@state", 0);
connection.Open();
try
{
mycmd.ExecuteNonQuery();
}
catch (Exception ee)
{
context.Response.Write(ee.Message);
return;
}
connection.Close();
}
public bool IsReusable
{
get
{
return false;
}
}
}
[解决办法]
请帖详细错误信息。
另外
type: "Get", 改成Get,因为你没有post任何数据
//dataType: "json", 注销掉,没有post参数