To implement an Ajax call in WebService, add this code:
To allow the use of ASP.net AJAX to invoke this Web service from a script, uncomment the downlink.
[System.Web.Script.Services.ScriptService]
Code Download/201008/yuanma/webservice2.rar
Copy Code code as follows:
No parameter calls
$ (document). Ready (function () {
$ (' #btn1 '). Click (function () {
$.ajax ({
Type: "POST",//Access WebService use POST method request
ContentType: "Application/json",//webservice returns JSON type
Url:webserviceurl + "Webservice1.asmx/helloworld",//Call WebService address and method name combination----Wsurl/Method name
Data: "{}",//here is the parameter to pass, format is data: "{paraname:paravalue}", you will see the following
DataType: ' JSON ',
Success:function (Result) {//callback function, result, return value
$ (' #dictionary '). Append (RESULT.D);
}
});
});
});
Call with parameters
$ (document). Ready (function () {
$ ("#btn2"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
Url:webserviceurl + "Webservice1.asmx/getwish",
Data: "{value1: ' All things are done ', value2: ' Good luck ', value3: ' Cattle and Cows ', value4:2009}",
DataType: ' JSON ',
Success:function (Result) {
$ (' #dictionary '). Append (RESULT.D);
}
});
});
});
Return collection (referenced from network, very descriptive)
$ (document). Ready (function () {
$ ("#btn3"). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
Url:webserviceurl + "Webservice1.asmx/getarray",
Data: "{i:10}",
DataType: ' JSON ',
Success:function (Result) {
$ (RESULT.D). each (function () {
alert (this);
$ (' #dictionary '). Append (this.tostring () + "");
Alert (Result.d.join ("|"));
});
}
});
});
});
Return compound type
$ (document). Ready (function () {
$ (' #btn4 '). Click (function () {
$.ajax ({
Type: "POST",
ContentType: "Application/json",
Url:webserviceurl + "Webservice1.asmx/getclass",
Data: "{}",
DataType: ' JSON ',
Success:function (Result) {
$ (RESULT.D). each (function () {
alert (this);
$ (' #dictionary '). Append (this[' ID '] + "" + this[' Value ']);
Alert (Result.d.join ("|"));
});
}
});
});
});
return DataSet (XML)
$ (document). Ready (function () {
$ (' #btn5 '). Click (function () {
$.ajax ({
Type: "POST",
Url:webserviceurl + "Webservice1.asmx/getdataset",
Data: "{}",
DataType: ' xml ',//return type is XML, and the preceding JSON, not the same
Success:function (Result) {
Demo Capture
try {
$ (Result). Find ("Table1"). each (function () {
$ (' #dictionary '). Append ($ (this). Find ("ID"). Text () + "" + $ (a). Find ("Value"). Text ());
});
}
catch (e) {
Alert (e);
Return
}
},
Error:function (result, status) {//If no above catch error will execute the callback function here
if (Status = = ' Error ') {
alert (status);
}
}
});
});
});
Ajax provides feedback to users, using the Ajaxstart and Ajaxstop methods to demonstrate Ajax tracking callbacks to related events, both of which can be added to jquery objects before and after Ajax callbacks
But the monitoring of Ajax is in itself a global
$ (document). Ready (function () {
$ (' #loading '). Ajaxstart (function () {
$ (this). Show ();
}). Ajaxstop (function () {
$ (this). Hide ();
});
});