jQuery AJax调用asp.net webservers 方法

 

aspx页面代码

<html xmlns="http://www.w3.org/1999/xhtml" >  
  1. <head runat="server">  
  2.   <title></title>  
  3.   <mce:script src="JQUERY.JS" mce_src="JQUERY.JS" type="text/javascript"></mce:script>  
  4.   <mce:style type="text/css"><!--  
  5. .hover  
  6. {  
  7. cursor: pointer; /*小手*/  
  8. background: #ffc; /*背景*/  
  9. }  
  10. .button  
  11. {  
  12. width: 150px;  
  13. float: left;  
  14. text-align: center;  
  15. margin: 10px;  
  16. padding: 10px;  
  17. border: 1px solid #888;  
  18. }  
  19. #dictionary  
  20. {  
  21. text-align: center;  
  22. font-size: 18px;  
  23. clear: both;  
  24. border-top: 3px solid #888;  
  25. }  
  26. #loading  
  27. {  
  28. border: 1px #000 solid;  
  29. background-color: #eee;  
  30. padding: 20px;  
  31. margin: 100px 0 0 200px;  
  32. position: absolute;  
  33. display: none;  
  34. }  
  35.     
  36. --></mce:style><mce:style type="text/css" mce_bogus="1"><!--  
  37. .hover  
  38. {  
  39. cursor: pointer; /*小手*/  
  40. background: #ffc; /*背景*/  
  41. }  
  42. .button  
  43. {  
  44. width: 150px;  
  45. float: left;  
  46. text-align: center;  
  47. margin: 10px;  
  48. padding: 10px;  
  49. border: 1px solid #888;  
  50. }  
  51. #dictionary  
  52. {  
  53. text-align: center;  
  54. font-size: 18px;  
  55. clear: both;  
  56. border-top: 3px solid #888;  
  57. }  
  58. #loading  
  59. {  
  60. border: 1px #000 solid;  
  61. background-color: #eee;  
  62. padding: 20px;  
  63. margin: 100px 0 0 200px;  
  64. position: absolute;  
  65. display: none;  
  66. }  
  67.     
  68. --></mce:style><style type="text/css" mce_bogus="1" mce_bogus="1">.hover  
  69. {  
  70. cursor: pointer; /*小手*/  
  71. background: #ffc; /*背景*/  
  72. }  
  73. .button  
  74. {  
  75. width: 150px;  
  76. float: left;  
  77. text-align: center;  
  78. margin: 10px;  
  79. padding: 10px;  
  80. border: 1px solid #888;  
  81. }  
  82. #dictionary  
  83. {  
  84. text-align: center;  
  85. font-size: 18px;  
  86. clear: both;  
  87. border-top: 3px solid #888;  
  88. }  
  89. #loading  
  90. {  
  91. border: 1px #000 solid;  
  92. background-color: #eee;  
  93. padding: 20px;  
  94. margin: 100px 0 0 200px;  
  95. position: absolute;  
  96. display: none;  
  97. }  
  98.   </style>  
  99.   <mce:script type="text/javascript"><!--  
  100.     //无参数调用  
  101.     $(document).ready(function() {  
  102.       $('#btn1').click(function() {  
  103.         $.ajax({  
  104.           type: "POST",  //访问WebService使用Post方式请求  
  105.           contentType: "application/json", //WebService 会返回Json类型  
  106.           url: "WebService1.asmx/HelloWorld", //调用WebService的地址和方法名称组合 ---- WsURL/方法名  
  107.           data: "{}",     //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到     
  108.           dataType: 'json',  
  109.           success: function(result) {   //回调函数,result,返回值  
  110.             $('#dictionary').append(result.d);  
  111.           }  
  112.         });  
  113.       });  
  114.     });  
  115.     //有参数调用  
  116.     $(document).ready(function() {  
  117.       $("#btn2").click(function() {  
  118.         $.ajax({  
  119.           type: "POST",  
  120.           contentType: "application/json",  
  121.           url: "WebService1.asmx/GetWish",  
  122.           data: "{value1:'心想事成',value2:'万事如意',value3:'牛牛牛',value4:2009}",  
  123.           dataType: 'json',  
  124.           success: function(result) {  
  125.             $('#dictionary').append(result.d);  
  126.           }  
  127.         });  
  128.       });  
  129.     });  
  130.       
  131.       
  132.     //返回集合(引用自网络,很说明问题)  
  133.     $(document).ready(function() {  
  134.       $("#btn3").click(function() {  
  135.         $.ajax({  
  136.           type: "POST",  
  137.           contentType: "application/json",  
  138.           url: "WebService1.asmx/GetArray",  
  139.           data: "{i:10}",  
  140.           dataType: 'json',  
  141.           success: function(result) {  
  142.             $(result.d).each(function() {  
  143.               //alert(this);  
  144.               $('#dictionary').append(this.toString() + " ");  
  145.               //alert(result.d.join(" | "));  
  146.             });  
  147.           }  
  148.         });  
  149.       });  
  150.     });  
  151.     //返回复合类型  
  152.     $(document).ready(function() {  
  153.       $('#btn4').click(function() {  
  154.         $.ajax({  
  155.           type: "POST",  
  156.           contentType: "application/json",  
  157.           url: "WebService1.asmx/GetClass",  
  158.           data: "{}",  
  159.           dataType: 'json',  
  160.           success: function(result) {  
  161.             $(result.d).each(function() {  
  162.               //alert(this);  
  163.               $('#dictionary').append(this['ID'] + " " + this['Value']);  
  164.               //alert(result.d.join(" | "));  
  165.             });  
  166.           }  
  167.         });  
  168.       });  
  169.     });  
  170.     //返回DataSet(XML)  
  171.     $(document).ready(function() {  
  172.       $('#btn5').click(function() {  
  173.         $.ajax({  
  174.           type: "POST",  
  175.           url: "WebService1.asmx/GetDataSet",  
  176.           data: "{}",  
  177.           dataType: 'xml', //返回的类型为XML ,和前面的Json,不一样了  
  178.           success: function(result) {  
  179.           //演示一下捕获  
  180.             try {   
  181.               $(result).find("Table1").each(function() {  
  182.                 $('#dictionary').append($(this).find("ID").text() + " " + $(this).find("Value").text());  
  183.               });  
  184.             }  
  185.             catch (e) {  
  186.               alert(e);  
  187.               return;  
  188.             }  
  189.           },  
  190.           error: function(result, status) { //如果没有上面的捕获出错会执行这里的回调函数  
  191.             if (status == 'error') {  
  192.               alert(status);  
  193.             }  
  194.           }  
  195.         });  
  196.       });  
  197.     });  
  198.     //Ajax 为用户提供反馈,利用ajaxStart和ajaxStop 方法,演示ajax跟踪相关事件的回调,他们两个方法可以添加给jQuery对象在Ajax前后回调  
  199.     //但对与Ajax的监控,本身是全局性的  
  200.     $(document).ready(function() {  
  201.       $('#loading').ajaxStart(function() {  
  202.         $(this).show();  
  203.       }).ajaxStop(function() {  
  204.         $(this).hide();  
  205.       });  
  206.     });  
  207.     // 鼠标移入移出效果,多个元素的时候,可以使用“,”隔开  
  208.     $(document).ready(function() {  
  209.       $('div.button').hover(function() {  
  210.         $(this).addClass('hover');  
  211.       }, function() {  
  212.         $(this).removeClass('hover');  
  213.       });  
  214.     });  
  215.       
  216.       
  217.     
  218. // --></mce:script>  
  219. </head>  
  220. <body>  
  221.   <form id="form1" runat="server">  
  222.   <div id="switcher">  
  223.     <h2>  
  224.       jQuery 的WebServices 调用</h2>  
  225.     <div class="button" id="btn1">  
  226.       HelloWorld</div>  
  227.     <div class="button" id="btn2">  
  228.       传入参数</div>  
  229.     <div class="button" id="btn3">  
  230.       返回集合</div>  
  231.     <div class="button" id="btn4">  
  232.       返回复合类型</div>  
  233.     <div class="button" id="btn5">  
  234.       返回DataSet(XML)</div>  
  235.   </div>  
  236.   <div id="loading">  
  237.     服务器处理中,请稍后。  
  238.   </div>  
  239.   <div id="dictionary">  
  240.   </div>  
  241.   </form>  
  242. </body>  
  243. </html>  

 

WebService1.asmx 代码

using System;  
  1. using System.Collections;  
  2. using System.ComponentModel;  
  3. using System.Data;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Services;  
  7. using System.Web.Services.Protocols;  
  8. using System.Xml.Linq;  
  9. using System.Collections.Generic;  
  10. namespace jquery_Learning  
  11. {  
  12.     /// <summary>   
  13.     /// WebService1 的摘要说明   
  14.     /// </summary>   
  15.     [WebService(Namespace = "http://tempuri.org/")]  
  16.     [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  17.     [System.ComponentModel.ToolboxItem(false)]  
  18.     // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。   
  19.     [System.Web.Script.Services.ScriptService]  
  20.     public class WebService1 : System.Web.Services.WebService  
  21.     {  
  22.         /// <summary>   
  23.         /// 无参数   
  24.         /// </summary>   
  25.         /// <returns></returns>   
  26.         [WebMethod]  
  27.         public string HelloWorld()  
  28.         {  
  29.             return "Hello World ";  
  30.         }  
  31.         /// <summary>   
  32.         /// 带参数   
  33.         /// </summary>   
  34.         /// <param name="value1"></param>   
  35.         /// <param name="value2"></param>   
  36.         /// <param name="value3"></param>   
  37.         /// <param name="value4"></param>   
  38.         /// <returns></returns>   
  39.         [WebMethod]  
  40.         public string GetWish(string value1, string value2, string value3, int value4)  
  41.         {  
  42.             return string.Format("祝您在{3}年里 {0}、{1}、{2}", value1, value2, value3, value4);  
  43.         }  
  44.         /// <summary>   
  45.         /// 返回集合   
  46.         /// </summary>   
  47.         /// <param name="i"></param>   
  48.         /// <returns></returns>   
  49.         [WebMethod]  
  50.         public List<int> GetArray(int i)  
  51.         {  
  52.             List<int> list = new List<int>();  
  53.             while (i >= 0)  
  54.             {  
  55.                 list.Add(i--);  
  56.             }  
  57.             return list;  
  58.         }  
  59.         /// <summary>   
  60.         /// 返回一个复合类型   
  61.         /// </summary>   
  62.         /// <returns></returns>   
  63.         [WebMethod]  
  64.         public Class1 GetClass()  
  65.         {  
  66.             return new Class1 { ID = "1", Value = "牛年大吉" };  
  67.         }  
  68.         /// <summary>   
  69.         /// 返回XML   
  70.         /// </summary>   
  71.         /// <returns></returns>   
  72.         [WebMethod]  
  73.         public DataSet GetDataSet()  
  74.         {  
  75.             DataSet ds = new DataSet();  
  76.             DataTable dt = new DataTable();  
  77.             dt.Columns.Add("ID", Type.GetType("System.String"));  
  78.             dt.Columns.Add("Value", Type.GetType("System.String"));  
  79.             DataRow dr = dt.NewRow();  
  80.             dr["ID"] = "1";  
  81.             dr["Value"] = "新年快乐";  
  82.             dt.Rows.Add(dr);  
  83.             dr = dt.NewRow();  
  84.             dr["ID"] = "2";  
  85.             dr["Value"] = "万事如意";  
  86.             dt.Rows.Add(dr);  
  87.             ds.Tables.Add(dt);  
  88.             return ds;  
  89.         }  
  90.     }  
  91.     //自定义的类,只有两个属性   
  92.     public class Class1  
  93.     {  
  94.         public string ID { getset; }  
  95.         public string Value { getset; }  
  96.     }  
  97. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值