您好,登錄后才能下訂單哦!
AJAX(Asynchronous JavaScript and XML)是一種在不重新加載整個(gè)頁(yè)面的情況下,與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁(yè)內(nèi)容的技術(shù)
創(chuàng)建一個(gè)ASP.NET Web Forms應(yīng)用程序。
在項(xiàng)目中添加一個(gè)新的Web Form,例如AjaxDemo.aspx
。
在AjaxDemo.aspx
中添加以下HTML和JavaScript代碼:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AJAX Demo</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
function getData() {
$.ajax({
type: "POST",
url: "AjaxDemo.aspx/GetData",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$("#result").html(response.d);
},
error: function (response) {
alert("Error: " + response.statusText);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>AJAX Demo</h1>
<button onclick="getData()">Get Data</button>
<p id="result"></p>
</div>
</form>
</body>
</html>
AjaxDemo.aspx.cs
文件中添加以下C#代碼:using System;
using System.Web.Services;
public partial class AjaxDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static string GetData()
{
return "Hello, AJAX!";
}
}
AjaxDemo.aspx
頁(yè)面。點(diǎn)擊"Get Data"按鈕,你將看到"Hello, AJAX!"消息顯示在頁(yè)面上,而無(wú)需刷新整個(gè)頁(yè)面。這個(gè)簡(jiǎn)單的示例展示了如何在C#項(xiàng)目中使用AJAX。你可以根據(jù)自己的需求擴(kuò)展此示例,例如從數(shù)據(jù)庫(kù)獲取數(shù)據(jù)、調(diào)用Web API等。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。