JQuery中ajax方法访问web服务
生活随笔
收集整理的這篇文章主要介紹了
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?}?
??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?}?
轉載于:https://www.cnblogs.com/dangqinghua/archive/2012/04/22/2465261.html
總結
以上是生活随笔為你收集整理的JQuery中ajax方法访问web服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: struts1 几种Action父类的比
- 下一篇: onbeforeunload与onunl