ASP.NET通过ajax调用后台方法
生活随笔
收集整理的這篇文章主要介紹了
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调用后台方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Struts2配置struts.xml文
- 下一篇: Ping命令检测网站运行状态