Ajax_请求get,post案例

1. 最原始的ajax请求方式

(1). get请求

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxDemo.aspx.cs" Inherits="ajax_AjaxDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGetDate").click(function () {
                //开始通过AJAX向服务器发送请求.
                var xhr;
                if (XMLHttpRequest) {//表示用户使用的高版本IE,谷歌,狐火等浏览器
                    xhr = new XMLHttpRequest();
                } else {// 低IE
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xhr.open("get", "GetDate.ashx?name=zhangsan&age=12", true);
                xhr.send();//开始发送
                //回调函数:当服务器将数据返回给浏览器后,自动调用该方法。
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4) {//表示服务端已经将数据完整返回,并且浏览器全部接受完毕。
                        if (xhr.status == 200) {//判断响应状态码是否为200.

                            alert(xhr.responseText);

                        }
                    }
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1"  runat="server">
    <div>
    <input type="button" value="获取服务端时间" id="btnGetDate" />
    </div>
    </form>
</body>
</html>

(2). post请求

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxPostDemo.aspx.cs" Inherits="ajax_AjaxPostDemo" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnPost").click(function () {
                var xhr;
                if (XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else {
                    xhr = new ActiveXObject("Microsoft.XMLHTTP");
                }
                xhr.open("post", "GetDate.ashx", true);
                //                      表示想服务端发送的请求都放在请求报文体中,并且以下面的形式发送出去                         
                xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xhr.send("name=zhangsan&pwd=123");
                xhr.onreadystatechange = function () {
                    if (xhr.readyState == 4) {
                        if (xhr.status == 200) {
                            alert(xhr.responseText);
                        }
                    }
                }
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server" >
    <div>
    <input type="button" value="获取数据" id="btnPost" />
        
    </div>
    </form>
</body>
</html>

(3). 请求的公共ashx文件

<%@ WebHandler Language="C#" Class="GetDate" %>

using System;
using System.Web;
 public class GetDate : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //                       context.requrest该方法会自动判断请求的方式是get还是post
            context.Response.Write(context.Request["name"]);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
View Code

(4) .通过Jquery请求Ajax的方式

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JqueryAjax.aspx.cs" Inherits="ajax_JqueryAjax" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <script src="../js/jquery-1.7.1.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#btnGet").click(function () {                            //回调函数
                $.get("GetDate.ashx", { "name": "lisi", "pwd": "123" }, function (data) {
                    alert(data)
                });

            });

            $("#btnPost").click(function () {
                $.post("GetDate.ashx", { "name": "lisi", "pwd": "123" }, function (data) {
                    alert(data)
                })
            });


            $("#btnAjax").click(function () {
                $.ajax({
                    type: "POST",   //请求类型
                    url: "GetDate.ashx",   //请求地址
                    data: "name=John&location=Boston",   //请求参数
                    success: function (msg) {     //回调函数
                        alert("Data Saved: " + msg);
                    }
                });

            });


        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <input type="button" value="GET获取数据" id="btnGet" />
        <input type="button" value="POST获取数据" id="btnPost" />
        <input type="button" value="AJAX获取数据" id="btnAjax" />
    </div>
    </form>
</body>
</html>
View Code

 (5). serializeArray方法的使用

//将对象转成json对象传递给后端
$("button").click(function(){
        var par =$("form表单id的值").serializeArray();
        });
原文地址:https://www.cnblogs.com/wangjinya/p/10242847.html