C# 利用JQuery调用后台方法

 

页面代码:

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JQueryTest.aspx.cs" Inherits="MsChartTest.JQueryTest" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml" >  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <mce:script src="JS/jquery.js" mce_src="JS/jquery.js" type="text/javascript"></mce:script>  
  9.     <mce:script type="text/javascript"><!--  
  10.         $(function() {  
  11.             $("#btnOK").click(function() {  
  12.                 $.ajax({  
  13.                     type: "Post",  
  14.                     url: "JQueryTest.aspx/SayHello",  
  15.                     data: "{}",  
  16.                     contentType: "application/json; charset=utf-8",  
  17.                     dataType: "json",  
  18.                     success: function(data) {  
  19.                         alert(data.d);  
  20.                     },  
  21.                     error: function(err) {  
  22.                         alert(err);  
  23.                     }  
  24.                 });  
  25.                 return false;  
  26.             });  
  27.             $("#btnOK0").click(function() {  
  28.                 $.ajax({  
  29.                     type: "Post",  
  30.                     url: "JQueryTest.aspx/GetStr",  
  31.                     data: "{'str':'我是','str2':'XXX'}",  
  32.                     contentType: "application/json; charset=utf-8",  
  33.                     dataType: "json",  
  34.                     success: function(data) {  
  35.                         alert(data.d);  
  36.                     },  
  37.                     error: function(err) {  
  38.                         alert(err);  
  39.                     }  
  40.                 });  
  41.                 return false;  
  42.             });  
  43.             $("#btnOK1").click(function() {  
  44.                 $.ajax({  
  45.                     type: "Post",  
  46.                     url: "JQueryTest.aspx/GetArray",  
  47.                     data: "{}",  
  48.                     contentType: "application/json; chartset=utf-8",  
  49.                     dataType: "json",  
  50.                     success: function(data) {  
  51.                         $("#list").html("");  
  52.                         $(data.d).each(function() {  
  53.                             $("#list").append("<li>" + this + "</li>");  
  54.                         });  
  55.                         alert(data.d);  
  56.                     },  
  57.                     error: function(err) {  
  58.                         alert(err);  
  59.                     }  
  60.                 });  
  61.                 return false;  
  62.             });  
  63.             $("#btnOK2").click(function() {  
  64.                 $.ajax({  
  65.                     type: "Post",  
  66.                     url: "JQueryTest.aspx/GetHash",  
  67.                     data: "{'key':'haha','value':'哈哈!'}",  
  68.                     contentType: "application/json: charset=utf-8",  
  69.                     dataType: "json",  
  70.                     success: function(data) {  
  71.                         alert("key:haha==>" + data.d["haha"] + "/n key:www==>" + data.d["www"]);  
  72.                     },  
  73.                     error: function(err) {  
  74.                         alert(err + "err");  
  75.                     }  
  76.                 });  
  77.                 return false;  
  78.             });  
  79.             $("#btnOK3").click(function() {  
  80.                 $.ajax({  
  81.                     url: "XMLTest.xml",  
  82.                     dataType: 'xml',  
  83.                     success: function(xml) {  
  84.                         $("#list1").html("");  
  85.                         $(xml).find("data>item").each(function() {  
  86.                             $("#list1").append("<li>id:" + $(this).find("id").text() + "</li>");  
  87.                             $("#list1").append("<li>name:" + $(this).find("name").text() + "</li>");  
  88.                         })  
  89.                     },  
  90.                     error: function(result, status) {  
  91.                         alert(status);  
  92.                     }  
  93.                 });  
  94.                 return false;  
  95.             });  
  96.         });   
  97.       
  98. // --></mce:script>  
  99. </head>  
  100. <body>  
  101.     <form id="form1" runat="server">  
  102.     <div>  
  103.         <input id="btnOK" type="button" value="button" />  
  104.         <input id="btnOK0" type="button" value="button" />  
  105.         <input id="btnOK1" type="button" value="button" />  
  106.         <input id="btnOK2" type="button" value="button" />  
  107.         <input id="btnOK3" type="button" value="button" />  
  108.         <ul id="list"></ul>    
  109.         <ul id="list1"></ul>   
  110.     </div>  
  111.     </form>  
  112. </body>  
  113. </html>  

 

后台代码:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Web.Script.Services;  
  8. using System.Web.Services;  
  9. using System.Collections;  
  10.   
  11. namespace MsChartTest  
  12. {  
  13.     public partial class JQueryTest : System.Web.UI.Page  
  14.     {  
  15.         protected void Page_Load(object sender, EventArgs e)  
  16.         {  
  17.   
  18.         }  
  19.   
  20.         [WebMethod]  
  21.         public static string SayHello()  
  22.         {  
  23.             return "Hello Ajax";  
  24.         }  
  25.   
  26.         [WebMethod]  
  27.         public static string GetStr(string str, string str2)  
  28.         {  
  29.             return str + str2;  
  30.         }  
  31.   
  32.         [WebMethod]  
  33.         public static List<string> GetArray()  
  34.         {  
  35.             List<string> li = new List<string>();  
  36.             for (int i = 0; i < 10; i++)  
  37.             {  
  38.                 li.Add(i + "");  
  39.             }  
  40.             return li;  
  41.         }  
  42.   
  43.         [WebMethod]  
  44.         public static Hashtable GetHash(string key, string value)  
  45.         {  
  46.             Hashtable hs = new Hashtable();  
  47.             hs.Add("www""yahooo");  
  48.             hs.Add(key, value);  
  49.             return hs;  
  50.         }  
  51.     }  
  52. }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bzhyan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值