感谢balexandre和rtiq,我的工作流程已经全部解决。我的.ashx文件正在被调用,所以我知道部分代码正在运行,并且警告我错误。当我跟踪.NET时,通过context.Request[“ email”]和context.Request [“ optin”]引入的变量为NULL。
我知道有什么问题,但看不到。我重新编辑了这篇文章,以获取最新的代码。
HEAD中的jQuery
$(document).ready(function () {
$(".submitConnectButton").click(function (evt) {
evt.preventDefault();
alert("hello click");
alert($(".emailConnectTextBox").val());
$.ajax({
type: "POST",
url: "/asynchronous/insertEmail.ashx",
data: "{email: '" + $(".emailConnectTextBox").val() + "',optin: '" + $(".connectCheckbox").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) { alert(msg.d); },
error: function (msg) { alert('Error:' + msg); }
});
});
});
HTML
.ashx中的CodeBehind
public class insertEmail : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
string strConnection = System.Configuration.ConfigurationManager.AppSettings["SQLConnectString"].ToString();
string email = context.Request["email"],
optin = context.Request["optin"];
string strSQL = "INSERT INTO Emails (emailAddress,optIn) VALUES('" + email.ToString() + "','" + optin.ToString() + "')";
SqlConnection Conn = new SqlConnection(strConnection);
SqlCommand Command = new SqlCommand(strSQL, Conn);
Conn.Open();
Command.ExecuteNonQuery();
Conn.Close();
context.Response.ContentType = "text/plain";
context.Response.Write("email inserted");
}
public bool IsReusable
{
get
{
return false;
}
}
}
表单和元素运行正常。我们只是得到这个NULL值而不能插入。Ajax正在正确调用.ashx文件,并且文件正在编译,所请求的变量为null。以前的帮助太棒了,如果有人可以帮助我解决这一问题,那么您将获得一天的金星奖!:)
经过一些脱机搜索之后,终于与balexandres .aspx方法结合使用了:
解
$.post("/asynchronous/addEmail.aspx", {email: $(".emailConnectTextBox").val(),optin: $(".connectCheckbox").is(':checked')}, function(data) { alert('Successful Submission');});