利用JQuery的$.ajax()能夠很方便的調用asp.net的後臺方法。

先來個簡單的實例熱熱身吧。javascript

一、無參數的方法調用html

asp.net code:java

[c-sharp]  view plain copy
  1. using System.Web.Script.Services;  
  2.   
  3. [WebMethod]  
  4. public static string SayHello()  
  5. {  
  6.      return "Hello Ajax!";  
  7. }  

注意:1.方法必定要靜態方法,並且要有[WebMethod]的聲明jquery

 

JQuery code:ajax

[javascript]  view plain copy
  1. /// <reference path="jquery-1.4.2-vsdoc.js"/>  
  2. $(function() {  
  3.     $("#btnOK").click(function() {  
  4.         $.ajax({  
  5.             //要用post方式  
  6.             type: "Post",  
  7.             //方法所在頁面和方法名  
  8.             url: "data.aspx/SayHello",  
  9.             contentType: "application/json; charset=utf-8",  
  10.             dataType: "json",  
  11.             success: function(data) {  
  12.                 //返回的數據用data.d獲取內容  
  13.                 alert(data.d);  
  14.             },  
  15.             error: function(err) {  
  16.                 alert(err);  
  17.             }  
  18.         });  
  19.   
  20.         //禁用按鈕的提交  
  21.         return false;  
  22.     });  
  23. });  

 

結果:json

 

 

二、帶參數的方法調用數組

asp.net code:app

[c-sharp]  view plain copy
  1. using System.Web.Script.Services;  
  2.   
  3. [WebMethod]  
  4. public static string GetStr(string str, string str2)  
  5. {  
  6.     return str + str2;  
  7. }  

 

JQuery code:asp.net

[javascript]  view plain copy
  1. /// <reference path="jquery-1.4.2-vsdoc.js"/>  
  2. $(function() {  
  3.     $("#btnOK").click(function() {  
  4.         $.ajax({  
  5.             type: "Post",  
  6.             url: "data.aspx/GetStr",  
  7.             //方法傳參的寫法必定要對,str爲形參的名字,str2爲第二個形參的名字  
  8.             data: "{'str':'我是','str2':'XXX'}",  
  9.             contentType: "application/json; charset=utf-8",  
  10.             dataType: "json",  
  11.             success: function(data) {  
  12.                 //返回的數據用data.d獲取內容  
  13.                   alert(data.d);  
  14.             },  
  15.             error: function(err) {  
  16.                 alert(err);  
  17.             }  
  18.         });  
  19.   
  20.         //禁用按鈕的提交  
  21.         return false;  
  22.     });  
  23. });  

 

運行結果:函數

 

 

下面進入高級應用羅

 

三、返回數組方法的調用

 asp.net code:

[c-sharp]  view plain copy
  1. using System.Web.Script.Services;  
  2.   
  3. [WebMethod]  
  4. public static List<string> GetArray()  
  5. {  
  6.     List<string> li = new List<string>();  
  7.   
  8.     for (int i = 0; i < 10; i++)  
  9.         li.Add(i + "");  
  10.   
  11.     return li;  
  12. }  

 

JQuery code:

 

[javascript]  view plain copy
  1. /// <reference path="jquery-1.4.2-vsdoc.js"/>  
  2. $(function() {  
  3.     $("#btnOK").click(function() {  
  4.         $.ajax({  
  5.             type: "Post",  
  6.             url: "data.aspx/GetArray",  
  7.             contentType: "application/json; charset=utf-8",  
  8.             dataType: "json",  
  9.             success: function(data) {  
  10.                 //插入前先清空ul  
  11.                 $("#list").html("");  
  12.   
  13.                 //遞歸獲取數據  
  14.                 $(data.d).each(function() {  
  15.                     //插入結果到li裏面  
  16.                     $("#list").append("<li>" + this + "</li>");  
  17.                 });  
  18.   
  19.                 alert(data.d);  
  20.             },  
  21.             error: function(err) {  
  22.                 alert(err);  
  23.             }  
  24.         });  
  25.   
  26.         //禁用按鈕的提交  
  27.         return false;  
  28.     });  
  29. });  

 

運行結果:

 

四、返回Hashtable方法的調用

asp.net code:

[c-sharp]  view plain copy
  1. using System.Web.Script.Services;  
  2. using System.Collections;  
  3.   
  4. [WebMethod]  
  5. public static Hashtable GetHash(string key,string value)  
  6. {  
  7.     Hashtable hs = new Hashtable();  
  8.   
  9.     hs.Add("www""yahooooooo");  
  10.     hs.Add(key, value);  
  11.       
  12.     return hs;  
  13. }  

 

JQuery code:

[javascript]  view plain copy
  1. /// <reference path="jquery-1.4.2-vsdoc.js"/>  
  2. $(function() {  
  3.     $("#btnOK").click(function() {  
  4.         $.ajax({  
  5.             type: "Post",  
  6.             url: "data.aspx/GetHash",  
  7.             //記得加雙引號  T_T  
  8.             data: "{ 'key': 'haha', 'value': '哈哈!' }",  
  9.             contentType: "application/json; charset=utf-8",  
  10.             dataType: "json",  
  11.             success: function(data) {  
  12.                 alert("key: haha ==> "+data.d["haha"]+"/n key: www ==> "+data.d["www"]);  
  13.             },  
  14.             error: function(err) {  
  15.                 alert(err + "err");  
  16.             }  
  17.         });  
  18.   
  19.         //禁用按鈕的提交  
  20.         return false;  
  21.     });  
  22. });  

 

 運行結果:

 

 

 

 

 五、操做xml

XMLtest.xml:

[xhtml]  view plain copy
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <data>  
  3.   <item>  
  4.     <id>1</id>  
  5.     <name>qwe</name>  
  6.   </item>  
  7.   <item>  
  8.     <id>2</id>  
  9.     <name>asd</name>  
  10.   </item>  
  11. </data>  

 

 

JQuery code:

 

[javascript]  view plain copy
  1. $(function() {  
  2.     $("#btnOK").click(function() {  
  3.         $.ajax({  
  4.             url: "XMLtest.xml",  
  5.             dataType: 'xml'//返回的類型爲XML ,和前面的Json,不同了  
  6.             success: function(xml) {  
  7.                 //清空list  
  8.                 $("#list").html("");  
  9.                 //查找xml元素  
  10.                 $(xml).find("data>item").each(function() {  
  11.                     $("#list").append("<li>id:" + $(this).find("id").text() +"</li>");  
  12.                     $("#list").append("<li>Name:"+ $(this).find("name").text() + "</li>");  
  13.                 })  
  14.             },  
  15.             error: function(result, status) { //若是沒有上面的捕獲出錯會執行這裏的回調函數  
  16.                 alert(status);  
  17.             }  
  18.         });  
  19.   
  20.         //禁用按鈕的提交  
  21.         return false;  
  22.     });  
  23. });  

 

運行結果:

相關文章
相關標籤/搜索