Ajax methods in jquery Access Web service instance _jquery

Source: Internet
Author: User

This example describes the AJAX approach to accessing Web services in jquery. Share to everyone for your reference. The specific analysis is as follows:

Description: ArrayList is stored as an object Collegedepartinfo its properties are: stirng collegedeparttitle and int collegedepartid in JavaScript Ddldepart.options[ddldepart.length]=new Option (N.collegedeparttitle,n.collegedepartid); The option parameters are based on their. Finally, it is important that [ScriptService] added above the class must be added, otherwise Ajax cannot invoke WebService

jquery Code section:

$.ajax ({
type: POST),
//Note returns JSON
contentType: "Application/json;utf-8",
// Collegedepartwebservices.asmx Web Service Name/getcollegedepart method name
URL: "collegedepartwebservices.asmx/ Getcollegedepart ",
//strdepartid parameter name collegeid parameter value
data:" {strdepartid: "+collegeid+"} ",
DataType:" JSON,
success:function (result) {          
  var json=null
   try
    {
    if (result)
    {
      // Because the return is ArrayList so the loop takes out the value
      $.each (result, function (i, n) {
      //ddldepart for the down menu. Loop to add new options to the Drop-down menu
      ddldepart.options[ddldepart.length]=new option (n.collegedeparttitle,n.collegedepartid);
    }
    }
    catch (E)
    {
     alert ("Error >>" +e.message);
     return;
    }
   ,
   error:function (data)
   {
   alert (data.status+ ">>>" +data.statustext) ;
   }
}); 

CollegeDepartWebServices.asmx.cs section:

[WebService (Namespace = "http://tempuri.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[ScriptService]
public class CollegeDepartWebServices:System.Web.Services.WebService
{public
  collegedepartwebservices ()
  {
    //If you are using a design 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.getcollegedepartlistbycollegeid (int. Parse (Strdepartid));
    Return myList
  }
}

I hope this article will help you with your jquery programming.

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.