感谢balexandre和rtiq,我的工作流程已经全部解决。我的.ashx文件正在被调用,所以我知道部分代码正在运行,并且警告我错误。当我跟踪.NET时,通过context.Request[“ email”]和context.Request [“ optin”]引入的变量为NULL。
我知道有什么问题,但看不到。我重新编辑了这篇文章,以获取最新的代码。
<script type="text/javascript"> $(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); } }); }); }); </script>
<div class="emailConnect"> <asp:TextBox runat="server" ID="TextBox1" CssClass="emailConnectTextBox" BorderStyle="Solid"></asp:TextBox> <asp:ImageButton id="connectButton" CssClass="submitConnectButton" runat="server" ImageUrl="~/Images/submit_btn.png" /><br /> <asp:CheckBox Checked="true" id="checkbox1" runat="server" CssClass="connectCheckbox" /> </div>
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');});
asynchronous
aspx
addEmail.aspx
。
public void Page_Load(...) { insertEmail(); } public void inserEmail() { string email = Request["email"], optin = Request["optin"]; string strSQL = "INSERT INTO Emails (emailAddress,optIn) VALUES('" + email.ToString() + "', optin)"; SqlConnection Conn = new SqlConnection(strConnection); SqlCommand Command = new SqlCommand(strSQL, Conn); Conn.Open(); Command.ExecuteNonQuery(); Conn.Close(); // Output Response.Write("email inserted"); }
.ajax()
url
url: "/asynchronous/insertEmail.aspx",
您将msg在success: function (msg) {}字符串中email inserted
msg
success: function (msg) {}
email inserted
不过,这是我一直在做的事情,而不是创建ASPX页面,而是使用不包含任何ASP.NET页面周期(加载速度更快)的ASHX(通用处理程序)页面,它是一个简单的页面。
如果要使用a Generic Handler,则在内部asynchronous文件夹中创建一个名为的文件inserEmail.ashx,完整代码为:
Generic Handler
inserEmail.ashx
public class insertEmail : IHttpHandler { public void ProcessRequest(HttpContext context) { string email = context.Request["email"], optin = context.Request["optin"]; string strSQL = "INSERT INTO Emails (emailAddress,optIn) VALUES('" + email.ToString() + "', optin)"; 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; } } }
并且,记得将您的url财产更改为url: "/asynchronous/insertEmail.ashx",
url: "/asynchronous/insertEmail.ashx",
从您的评论中,我意识到您的data财产也不正确。
data
正确的是:
data: { "email" : $(".emailConnectTextBox").val(), "optin" : $(".connectCheckbox").val() },
您的完整ajax调用应为:
$.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.d); } });
并且您Response.Write在通用处理程序中也应传递JSON字符串
Response.Write
因此,将tgis更改context.Response.Write("emailinserted");为context.Response.Write("{d:'email inserted'});
context.Response.Write("emailinserted");
context.Response.Write("{d:'email inserted'});
就这样。