关于我们

质量为本、客户为根、勇于拼搏、务实创新

< 返回新闻公共列表

提交数据到后台的C#代码

发布时间:2019-11-21 14:56:11

b.ashx后台处理程序

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using DTcms.Common;

using System.Data;

using System.Data.Sql;

using System.Data.SqlClient;

using DTcms.DBUtility;

namespace DTcms.Web

{

    /// <summary>

    /// b 的摘要说明

    /// </summary>

    public class b : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

            context.Response.ContentType = "text/plain";

            //context.Response.Write("Hello World");

            //string namex= DTRequest.GetQueryString("namex");

            string namex = DTRequest.GetFormString("namex");

            string password = DTRequest.GetFormString("password");

            //context.Response.Write("insert into t (namex,password) values ('" + namex + "','" + namex + "')");

            //context.Response.End();

            //context.Response.Write(password);

            int aa=DbHelperSQL.ExecuteSql("insert into t (namex,password) values ('" + namex + "','" + password + "')");

            if (aa>0) //写入数据库成功

            {

                context.Response.Write("{\"zt\":1,\"xx\":\"添加成功啦\"}");

            }

            else

            {

            }

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

}


前台简单提交例子

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title></title>

</head>

<body>

    <form method="get" action="b.ashx">

        <input id="hhhh" name="namex" type="text" value="" />

        <input name="password" type="password" value="" />

        <input type="submit" value="提交" />

    </form>

</body>

</html>


前台ajax提交的例子

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title></title>

</head>

<script type="text/javascript" src="http://www.kanmogu.com/scripts/jquery/jquery-1.10.2.min.js"></script>

<body>

    <script type="text/javascript">

        $(function () {

            //alert("初始化");

        });

        function tj() {

            alert($("#namex").val());

            $.ajax({

                type: "POST",

                url: "b.ashx",

                dataType: "json",

                timeout: 20000,

                data: {

                    "namex": $("#namex").val(), //

                    "password": $("#password").val(), //

                },

                beforeSend: function () {

                    $("#tjq").append("<img src=http://www.yn137.com/images/newlogo.gif >");

                    alert("提交前");

                },

                success: function (data, textStatus) {

                    if (data.zt == 1) {

                        $("#tjq").hide();

                        alert(data.zt);

                        alert(data.xx);

                    }

                },

                error: function (XMLHttpRequest, textStatus, errorThrown) {

                    //alert("错误");

                    //alert(XMLHttpRequest.status);

                    //alert(XMLHttpRequest.readyState);

                    //alert(textStatus); // paser error;

                }

            });

        }

    </script>

    <div id="tjq"></div>

    <input name="namex" id="namex" type="text" value="" />

    <input name="password" type="password" value="" />

    <input type="submit" onclick="tj()" value="提交" />

</body>

</html>



/template/Home/Zkeys/PC/Static