Ajax_請求get,post案例

1. 最原始的ajax請求方式javascript

(1). get請求html

<%@ 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請求java

<%@ 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文件jquery

<%@ 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的方式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

//將對象轉成json對象傳遞給後端
$("button").click(function(){ var par =$("form表單id的值").serializeArray(); });
相關文章
相關標籤/搜索