通过ajax发送的数据不会写入数据库

时间:2012-12-24 07:38:19

标签: c# ajax httphandler

我通过ajax发送数据,以格式化保存并保存在我的数据库中。我正在通过JSON将数据发送到HttpHandler,它可以使用它。

function sendMail() {
    $.ajax({
        type: "POST",
        url: '<%= ResolveUrl("~/MailAction.ashx") %>',
        data: { act: "sendMail", to: $('#<%= txtMailTo.ClientID %>').val(), from: $('#<%= lblUserID.ClientID %>').text(), subject: $('#<%= txtMailSubject.ClientID %>').val(), message: $('#<%= txtMailMessage.ClientID %>').val() },
        success: window.location = "Inbox.aspx"
    });
}

不幸的是,我在HttpHandler上设置的断点似乎没有被命中,所以我无法调试下面的代码,但我没有看到任何问题:

    public void ProcessRequest(HttpContext context)
    {
        int mailid = 0;
        string action = context.Request["act"];

        MySqlContext db = new MySqlContext();

        string sql = "";
        List<MySqlParameter> args = new List<MySqlParameter>();

        switch (action)
        {
            case "sendMail":
                string to = context.Request["to"];
                int from = int.Parse(context.Request["from"]);
                string subject = context.Request["subject"];

                //
                // FORMAT TEXT FROM MESSAGE BODY
                //
                string message = formatText(context.Request["message"]);

                int userid = 0;
                //
                // GET RECIPIENT ID
                //
                sql = "select userid from users where username = @to";
                args.Add(new MySqlParameter() { ParameterName = "@to", MySqlDbType = MySqlDbType.VarChar, Value = to });

                MySqlDataReader dr = db.getReader(sql, args);

                if (dr.HasRows)
                {
                    dr.Read();

                    userid = dr.GetInt32("userid");
                }
                dr.Close();
                args.Clear();

                sql = "insert into mail(sender, recipient, datesent, subject, message, status) values (@me, @you, @date, @sub, @msg, @stat)";
                args.Add(new MySqlParameter() { ParameterName = "@me", MySqlDbType = MySqlDbType.Int32, Value = from });
                args.Add(new MySqlParameter() { ParameterName = "@you", MySqlDbType = MySqlDbType.Int32, Value = to });
                args.Add(new MySqlParameter() { ParameterName = "@date", MySqlDbType = MySqlDbType.DateTime, Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") });
                args.Add(new MySqlParameter() { ParameterName = "@sub", MySqlDbType = MySqlDbType.VarChar, Value = subject });
                args.Add(new MySqlParameter() { ParameterName = "@msg", MySqlDbType = MySqlDbType.VarChar, Value = message });
                break;
            }

        if (args.Count == 0)
        {
            db.execute(sql);
        }
        else
        {
            db.execute(sql, args);
        }
    }

我所能说的就是它没有插入到数据库中。

我使用相同的javascript将其他值传递给其他事件的处理程序,所有工作正常。大多数人会对数据库进行更新。

有人能看到这里的代码是否有任何问题? 在我看来,完全在服务器端执行此操作可能是更好的做法,所以如果这是一个更好的选择,那么我非常愿意放弃它。

提前致谢

1 个答案:

答案 0 :(得分:0)

'成功:window.location = "Inbox.aspx"'

立即评估

window.location = "Inbox.aspx",并在发出任何请求之前切换页面。

使用功能。