日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

ASP.NET通过ajax调用后台方法

發布時間:2025/3/20 asp.net 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET通过ajax调用后台方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ASP.NET應用程序,通過ajax調用后臺的方法

實現非常簡單,直接上代碼

頁面代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="ajax.WebForm1" %><!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="Source/jquery.min.js"></script> </head> <body><form id="form1" runat="server"><div><asp:Button ID="btn" runat="server" Text="click me" /><asp:Label ID="lab" runat="server" Text="" /></div></form> </body> </html><script type="text/javascript">// 文檔加載$(document).ready(function () {// 綁定事件$("#btn").bind("click", send);})function send() {$.ajax({type: "post",url: "WebForm1.aspx/send",contentType: "application/json; charset=utf-8",dataType: "json",success: function (data) {alert("調用成功!");},error: function (err) {alert("產生錯誤!");}});} </script>

后臺代碼

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Services; using System.Web.UI; using System.Web.UI.WebControls;namespace ajax {public partial class WebForm1 : System.Web.UI.Page{protected void Page_Load(object sender, EventArgs e){}[WebMethod]public static string send(){return "true";}} }

注意
前端頁面,添加jQuery的引用
后臺代碼,方法必須為靜態方法,而且,必須使用[WebMethod]進行聲明

總結

以上是生活随笔為你收集整理的ASP.NET通过ajax调用后台方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。