Ajax post 提交数据

实现功能:前端html页面,填入相应的预约信息提交到后台返回并返回信息

1.前端ajax代码

<script type="text/javascript">

    $(function () {

        //表单提交

        $("#btnSubmit").click(function () {

            var txtname = $("#txtname").val();

            var txttel = $("#txttel").val();

            var txtOrderDepartment = $("#txtOrderDepartment").val();

            var txtOrderTime = $("#txtOrderTime").val();

            var txtOrderDescription = $("#txtOrderDescription").val();

            if (txtname == null || txtname == "") {

                alert("用户名不能为空!");

            }

            else

            {

                $.ajax({

                    type: "POST",

                    url: "../../tools/submit_ajax.ashx?action=order_add",

                    dataType: "json",

                    data: { txtname: txtname, txttel: txttel, txtOrderDepartment: txtOrderDepartment, txtOrderTime: txtOrderTime, txtOrderDescription: txtOrderDescription },

                    success: function (data, msg) {

                        if (data.status == 1) {

                            alert(data.msg);

                        }

                        else {

                            alert(data.msg);

                        }

                    }

                });

            }

        

        });

    });

</script>

2.Ashx文件中相应代码

public void ProcessRequest(HttpContext context)

        {

            //取得处事类型

            string action = DTRequest.GetQueryString("action");

 

            switch (action)

            {

                case "comment_add": //提交评论

                    comment_add(context);

                    break;

                case "comment_list": //评论列表

                    comment_list(context);

                    break;

}

}

#region 用户预约=====================================

        private void order_add(HttpContext context)

        {

            DTcms.Model.article_comment model = new Model.article_comment();

            model.user_name = DTRequest.GetFormString("txtname").Trim();//姓名

            model.reply_content = DTRequest.GetFormString("txttel").Trim();

            model.add_time = DateTime.Now;//添加预约的时间

            model.reply_time = Convert.ToDateTime(DTRequest.GetFormString("txtOrderTime").Trim());

            model.content = DTRequest.GetFormString("txtOrderDepartment").Trim()+" | "+DTRequest.GetFormString("txtOrderDescription").Trim();//预约描述

            //DTRequest.GetFormString("txtOrderDepartment").Trim()+预约科室\

            model.channel_id = 27;

            string userip = DTRequest.GetIP();

            DTcms.BLL.article_comment bll = new BLL.article_comment();

         

            //保存注册信息

            if (siteConfig.commentstatus == 0)

            {

                context.Response.Write("{\"status\":0, \"msg\":\"系统故障,暂停预约!\"}");

                return;

            }

           

            //设置对应的状态

            int newId = bll.Add(model);

            if (newId < 1)

            {

                context.Response.Write("{\"status\":0, \"msg\":\"系统故障,请联系网站管理员!\"}");

                return;

            }

            else

            {

                context.Response.Write("{\"status\":1, \"url\":\"" + new Web.UI.BasePage().linkurl("index") + "\", \"msg\":\"预约成功!\"}");

            }

            return;

        }

你可能感兴趣的:(Ajax)