這篇文章主要介紹了JQuery中ajax方法訪問web服務(wù)的方法,實例分析了jquery中Ajax方法結(jié)合asp.net程序處理web訪問的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了JQuery中ajax方法訪問web服務(wù)。分享給大家供大家參考。具體分析如下:
說明: ArrayList 中 存為對象CollegeDepartInfo 其屬性為:stirng CollegeDepartTitle 和 int CollegeDepartId 在javascript中 ddlDepart.options[ddlDepart.length]=new Option(n.CollegeDepartTitle,n.CollegeDepartId); Option的參數(shù)就是依據(jù)他們的。 最后重要的是: 類上方添加的 [ScriptService] 必須添加,否則ajax無法調(diào)用WebService
jquery代碼部分:
- $.ajax({
- type: "POST",
- //注明 返回Json
- contentType:"application/json;utf-8",
- //CollegeDepartWebServices.asmx web服務(wù)名 /GetCollegeDepart 方法名
- url:"CollegeDepartWebServices.asmx/GetCollegeDepart",
- //strDepartId 參數(shù)名稱 collegeId 參數(shù)值
- data:"{strDepartId:"+collegeId+"}",
- dataType:"json",
- success:function(result){
- var json=null
- try
- {
- if(result)
- {
- //因為返回的是ArrayList 所以循環(huán)取出其中的值
- $.each(result, function(i, n){
- //ddlDepart 為下來菜單。循環(huán)的向下拉菜單中添加新的選項
- ddlDepart.options[ddlDepart.length]=new Option(n.CollegeDepartTitle,n.CollegeDepartId);
- });
- }
- }
- catch(e)
- {
- alert("錯誤>>"+e.message);
- return;
- }
- },
- error:function(data)
- {
- alert(data.status+">>> "+data.statusText);
- }
- });
CollegeDepartWebServices.asmx.cs部分:
- [WebService(Namespace = "http://tempuri.org/")]
- [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
- [ScriptService]
- public class CollegeDepartWebServices : System.Web.Services.WebService
- {
- public CollegeDepartWebServices()
- {
- //如果使用設(shè)計的組件,請取消注釋以下行
- //InitializeComponent();
- }
- [WebMethod]
- [System.Xml.Serialization.XmlInclude(typeof(CollegeDepartInfo))]
- public ArrayList GetCollegeDepart(string strDepartId)
- {
- CollegeDepartBL.FlushCollegeDepartCache();
- if (string.IsNullOrEmpty(strDepartId))
- return null;
- ArrayList myList = CollegeDepartBL.GetCollegeDepartListByCollegeID(int.Parse(strDepartId));
- return myList;
- }
- }
希望本文所述對大家的jquery程序設(shè)計有所幫助。
新聞熱點
疑難解答
圖片精選