首页 > 开发 > AJAX > 正文

JQuery中ajax方法访问web服务实例

2024-09-01 08:33:03
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了JQuery中ajax方法访问web服务的方法,实例分析了jquery中Ajax方法结合asp.net程序处理web访问的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了JQuery中ajax方法访问web服务。分享给大家供大家参考。具体分析如下:

说明: ArrayList 中 存为对象CollegeDepartInfo 其属性为:stirng CollegeDepartTitle 和 int CollegeDepartId 在javascript中 ddlDepart.options[ddlDepart.length]=new Option(n.CollegeDepartTitle,n.CollegeDepartId); Option的参数就是依据他们的。 最后重要的是: 类上方添加的 [ScriptService] 必须添加,否则ajax无法调用WebService

jquery代码部分:

 

 
  1. $.ajax({ 
  2. type: "POST", 
  3. //注明 返回Json 
  4. contentType:"application/json;utf-8", 
  5. //CollegeDepartWebServices.asmx web服务名 /GetCollegeDepart 方法名 
  6. url:"CollegeDepartWebServices.asmx/GetCollegeDepart", 
  7. //strDepartId 参数名称 collegeId 参数值 
  8. data:"{strDepartId:"+collegeId+"}", 
  9. dataType:"json", 
  10. success:function(result){  
  11. var json=null 
  12. try 
  13. { 
  14. if(result) 
  15. { 
  16. //因为返回的是ArrayList 所以循环取出其中的值 
  17. $.each(result, function(i, n){ 
  18. //ddlDepart 为下来菜单。循环的向下拉菜单中添加新的选项 
  19. ddlDepart.options[ddlDepart.length]=new Option(n.CollegeDepartTitle,n.CollegeDepartId); 
  20. }); 
  21. } 
  22. } 
  23. catch(e) 
  24. { 
  25. alert("错误>>"+e.message); 
  26. return; 
  27. } 
  28. }, 
  29. error:function(data) 
  30. { 
  31. alert(data.status+">>> "+data.statusText); 
  32. } 
  33. });  

CollegeDepartWebServices.asmx.cs部分:

 

 
  1. [WebService(Namespace = "http://tempuri.org/")] 
  2. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
  3. [ScriptService] 
  4. public class CollegeDepartWebServices : System.Web.Services.WebService 
  5. { 
  6. public CollegeDepartWebServices() 
  7. { 
  8. //如果使用设计的组件,请取消注释以下行  
  9. //InitializeComponent();  
  10. } 
  11. [WebMethod] 
  12. [System.Xml.Serialization.XmlInclude(typeof(CollegeDepartInfo))] 
  13. public ArrayList GetCollegeDepart(string strDepartId) 
  14. { 
  15. CollegeDepartBL.FlushCollegeDepartCache(); 
  16. if (string.IsNullOrEmpty(strDepartId)) 
  17. return null; 
  18. ArrayList myList = CollegeDepartBL.GetCollegeDepartListByCollegeID(int.Parse(strDepartId)); 
  19. return myList; 
  20. } 
  21. } 

希望本文所述对大家的jquery程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表