Use ajax in jQuery to access web services

Source: Internet
Author: User

Note: The ArrayList object CollegeDepartInfo has the following attributes: stirng college0000title and int CollegeDepartId in javascript: ddlDepart. options [ddlDepart. length] = new Option (n. collegeappstitle, n. collegeDepartId); Option parameters are based on them. Finally, the [ScriptService] added above the class must be added. Otherwise, ajax cannot call WebService.

JS Code:

$. Ajax ({
Type: "POST ",
// Specify the returned Json
ContentType: "application/json; UTF-8 ",
// Collegedomainwebservices. asmx web Service name/GetCollegeDepart method name
Url: "collegedomainwebservices. asmx/GetCollegeDepart ",
// StrDepartId parameter name collegeId parameter value
Data: "{strDepartId:" + collegeId + "}",
DataType: "json ",
Success: function (result ){
Var json = null
Try
{
If (result)
{
// The returned value is the ArrayList.
$. Each (result, function (I, n ){
// DdlDepart is the down menu. Add new options to the drop-down menu cyclically
DdlDepart. options [ddlDepart. length] = new Option (n. collegepartition title, n. CollegeDepartId );

});
}


}
Catch (e)
{
Alert ("error >>" + e. message );
Return;

}

},
Error: function (data)
{
Alert (data. status + ">>>" + data. statusText );
}
});

Collegedomainwebservices. asmx. cs

[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = WsiProfiles. BasicProfile1_1)]
[ScriptService]
Public class collegedomainwebservices: System. Web. Services. WebService
{

Public collegedomainwebservices ()
{

// If you use the designed component, uncomment the following line
// InitializeComponent ();
}

[WebMethod]
[System. Xml. Serialization. XmlInclude (typeof (CollegeDepartInfo)]
Public ArrayList GetCollegeDepart (string strDepartId)
{
CollegeDepartBL. FlushCollegeDepartCache ();

If (string. IsNullOrEmpty (strDepartId ))
Return null;

ArrayList myList = CollegeDepartBL. getcollege1_listbycollegeid (int. Parse (strDepartId ));
Return myList;

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.