x

开发者

c# .net

导航

JQuery中ajax方法访问web服务

  1 $.ajax({  
  2 
  3 02    type: "POST",  
  4 
  5 03    //注明 返回Json  
  6 
  7 04    contentType:"application/json;utf-8",  
  8 
  9 05    //CollegeDepartWebServices.asmx web服务名 /GetCollegeDepart 方法名  
 10 
 11 06    url:"CollegeDepartWebServices.asmx/GetCollegeDepart",  
 12 
 13 07       //strDepartId 参数名称 collegeId 参数值  
 14 
 15 08    data:"{strDepartId:"+collegeId+"}",  
 16 
 17 09    dataType:"json",  
 18 
 19 10    success:function(result){                      
 20 
 21 11     var json=null 
 22 
 23 12      try 
 24 
 25 13        {  
 26 
 27 14         if(result)  
 28 
 29 15         {  
 30 
 31 16             //因为返回的是ArrayList 所以循环取出其中的值  
 32 
 33 17             $.each(result, function(i, n){  
 34 
 35 18            //ddlDepart 为下来菜单。循环的向下拉菜单中添加新的选项  
 36 
 37 19            ddlDepart.options[ddlDepart.length]=new Option(n.CollegeDepartTitle,n.CollegeDepartId);  
 38 
 39 20                 
 40 
 41 21            });  
 42 
 43 22         }  
 44 
 45 23           
 46 
 47 24   
 48 
 49 25        }  
 50 
 51 26        catch(e)  
 52 
 53 27        {  
 54 
 55 28           alert("错误>>"+e.message);  
 56 
 57 29           return;  
 58 
 59 30              
 60 
 61 31        }  
 62 
 63 32          
 64 
 65 33      },  
 66 
 67 34      error:function(data)  
 68 
 69 35      {  
 70 
 71 36    alert(data.status+">>> "+data.statusText);  
 72 
 73 37      }  
 74 
 75 38 });   
 76 
 77 [代码] CollegeDepartWebServices.asmx.cs
 78 view sourceprint?01 [WebService(Namespace = "http://tempuri.org/")]  
 79 
 80 02 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
 81 
 82 03 [ScriptService]  
 83 
 84 04 public class CollegeDepartWebServices : System.Web.Services.WebService  
 85 
 86 05 {  
 87 
 88 06    
 89 
 90 07     public CollegeDepartWebServices()  
 91 
 92 08     {  
 93 
 94 09    
 95 
 96 10         //如果使用设计的组件,请取消注释以下行   
 97 
 98 11         //InitializeComponent();   
 99 
100 12     }  
101 
102 13    
103 
104 14     [WebMethod]  
105 
106 15     [System.Xml.Serialization.XmlInclude(typeof(CollegeDepartInfo))]  
107 
108 16     public ArrayList  GetCollegeDepart(string strDepartId)  
109 
110 17     {  
111 
112 18         CollegeDepartBL.FlushCollegeDepartCache();  
113 
114 19           
115 
116 20         if (string.IsNullOrEmpty(strDepartId))  
117 
118 21             return null;  
119 
120 22    
121 
122 23         ArrayList myList = CollegeDepartBL.GetCollegeDepartListByCollegeID(int.Parse(strDepartId));  
123 
124 24         return myList;  
125 
126 25    
127 
128 26     }  
129 
130 27    
131 
132 28 } 

posted on 2012-04-22 17:47  开发模式  阅读(384)  评论(0编辑  收藏  举报

x