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.