WebForm.aspx 頁面通過 AJAX 訪問WebForm.aspx.cs類中的方法,擷取資料,aspx.cs
WebForm.aspx 頁面通過 AJAX 訪問WebForm.aspx.cs類中的方法,擷取資料
WebForm1.aspx 頁面 (原生AJAX請求,寫法一)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="IsPostBack.WebForm1" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"> <title></title> <script src="jquery-1.11.2.js" type="text/javascript"></script> <script type="text/javascript"> function btnClick() { var xmlhttp = new XMLHttpRequest(); if (!xmlhttp) { alert("建立xmlhttp對象異常"); return false; } xmlhttp.open("POST", "WebForm1.aspx", true); xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { var str = xmlhttp.responseText; // 通過alert(str)得知 str={"Json":[{"Id":"1","UserName":"張三","Age":"25","Gender":"0"}]} //debugger; var obj = $.parseJSON(str); //將str這個字串轉換成Json對象 var name = obj.Json[0].UserName; //obj.Json取到的是[{"Id":"1","UserName":"張三","Age":"25","Gender":"0"}] 這個數組,數組裡只有一個值,即:{"Id":"1","UserName":"張三","Age":"25","Gender":"0"} 所以obj.Json[0]的值就是{"Id":"1","UserName":"張三","Age":"25","Gender":"0"} ; 而這個{"Id":"1","UserName":"張三","Age":"25","Gender":"0"} 是一個對象,所以可以通過 .UserName來獲得 "張三" 這個值 var age = obj.Json[0].Age; document.getElementById("name").innerHTML = name; document.getElementById("age").innerHTML = age; } else { alert("ajax伺服器錯誤"); } } } xmlhttp.send("id=1"); } </script></head><body> <form id="form1" runat="server"> <div> <table id="t1"border="1px"> <tr><td>姓名</td><td>年齡</td></tr> <tr><td id="name"></td><td id="age"></td></tr> </table> <input type="button" onclick="btnClick()" value="提交"/> </div> </form></body></html>
WebForm1.aspx 頁面 (原生AJAX請求,寫法二,一般情況下我們用這種)
<head runat="server"> <title></title> <script src="jquery-1.11.2.js" type="text/javascript"></script> <script type="text/javascript"> function btnClick() { $.ajax({ url: "WebForm1.aspx", type: "Post", dataType: "Text", //請求到伺服器返回的資料類型 data: { "id": "2" }, success: function (data) { var obj = $.parseJSON(data); //這個資料 var name = obj.Json[0].UserName; var age = obj.Json[0].Age; document.getElementById("name").innerHTML = name; document.getElementById("age").innerHTML = age; } }) } </script></head><body> <form id="form1" runat="server"> <div> <table id="t1"border="1px"> <tr><td>姓名</td><td>年齡</td></tr> <tr><td id="name"></td><td id="age"></td></tr> </table> <input type="button" onclick="btnClick()" value="提交"/> </div> </form></body></html>
WebForm1.aspx.cs如果你是try...catch裡面使用了Response.End()的話,會被捕捉到一個異常:線程被中止" 解決方案:請點擊
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Data;namespace IsPostBack{ public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string id = Request["id"]; //如果不是通過ajax 請求提交資料 就不會取到這個id ,所以此時的id 為null。但是如果是通過ajax請求提交資料,因為提交資料中有提交id,所以就能夠取到這個id ,此時的id就有值了。 if (!string.IsNullOrEmpty(id)) // 如果不是通過ajax 請求提交資料 就不會進入花括弧來調用GetUserData(string id) 方法 { GetUserData(id); //如果是通過ajax請求提交資料,就會進入花括弧來調用GetUserData(string id) 方法 } } void GetUserData(string id) { DataTable dt= SqlHelper.ExecuteDataTable("select * from T_User where id=@id", new SqlParameter("id", id)); string data= DataTableConvertJson.DataTableToJson("Json", dt); Response.Write(data); Response.End(); //將當前所有緩衝的輸出發送到用戶端,停止該頁的執行,如果沒有這一步的話,程式還會往後執行,除了把data輸出到用戶端之外,還會將webForm1.aspx裡面的html代碼都輸出到頁面。 } }}