首頁 > web前端 > js教程 > 主體

JQuery中ajax方法存取web服務實例_jquery

WBOY
發布: 2016-05-16 15:49:46
原創
1085 人瀏覽過

本文實例講述了JQuery中ajax方法存取web服務。分享給大家供大家參考。具體分析如下:

說明: ArrayList 中存為物件CollegeDepartInfo 其屬性為:stirng CollegeDepartTitle 和int CollegeDepartId 在javascript中ddlDepart.options[ddlDepart.length]=new Option(n.CollegeDepartTitle,n.CollealDepart.length]=new Option(n.CollegeDepartTitle,n.CollegeDepartId);的。 最後重要的是: 類別上方新增的 [ScriptService] 必須添加,否則ajax無法呼叫WebService

jquery程式碼部分:

$.ajax({
type: "POST",
//注明 返回Json
contentType:"application/json;utf-8",
//CollegeDepartWebServices.asmx web服务名 /GetCollegeDepart 方法名
url:"CollegeDepartWebServices.asmx/GetCollegeDepart",
//strDepartId 参数名称 collegeId 参数值
data:"{strDepartId:"+collegeId+"}",
dataType:"json",
success:function(result){          
  var json=null
   try
    {
    if(result)
    {
      //因为返回的是ArrayList 所以循环取出其中的值
      $.each(result, function(i, n){
      //ddlDepart 为下来菜单。循环的向下拉菜单中添加新的选项
      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()
  {
    //如果使用设计的组件,请取消注释以下行 
    //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程式設計有所幫助。

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!