您的位置:首页 > Web前端 > JQuery

Jquery+ajax的简单使用

2014-05-13 14:30 267 查看

//前台页面:

<script src="js/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">

        function JqQueryPostTest() {

            $.ajax({

                type: "post",

                url: "Test.ashx?type=post",

                data: "username=lisi&&age=90",

                datatype: "text",

                success: function(data) {

                    alert(data);

                },

                error: function(data) {

                    alert("失败!");

                }

            })

        }

        function JqQueryGetTest() {

            $.ajax({

                type: "get",

                url: "Test.ashx?type=get",

                data: "username=lisi&&age=90",

                datatype: "text",

                success: function(data) {

                    alert(data);

                },

                error: function(data) {

                    alert("失败!");

                }

            })

        }

    </script>

</head>

<body>

    <form id="form1" runat="server">

     <input type="text" id="username" />

     <br />

     <input type="text" id="age" />

     <br />

     <input type="button" value="JqQueryGetTest" id="btn" onclick="JqQueryGetTest();"/>

      <input type="button" value="JqQueryPostTest" id="Button1" onclick="JqQueryPostTest();"/>

    <div id="result">

    </div>

    </form>

</body>

.ashx 页面

public class Test1 : IHttpHandler

    {

        public void ProcessRequest(HttpContext context)

        {

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

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

            context.Response.Write(CheckPost(context));

           

        }

        public string CheckPost(HttpContext context)

        {

            string lei = context.Request["type"];

            string re = string.Empty;

            if (lei == "get")

            {

                string name = context.Request["username"];

                string age = context.Request["age"];

                re = "get方式访问("+name+"--"+age+")";

            }

            if (lei == "post")

            {

                string name = context.Request["username"];

                string age = context.Request["age"];

                re = "post方式访问(" + name + "--" + age + ")";

            }

            return re;

        }

        public bool IsReusable

        {

            get

            {

                return false;

            }

        }

    }

区别的话,

总之,GET方式传送数据量小(1KB左右),处理效率高,安全性低,会被缓存,而POST相反
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: