Add this sentence to implement Ajax calls in WebService. Code :
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. scriptservice]
Code download/201008/Yuanma/webservice2.rar
Copy code The Code is as follows: // call without Parameters
$ (Document). Ready (function (){
$ ('# Btn1'). Click (function (){
$. Ajax ({
Type: "Post", // WebService access request using post
Contenttype: "application/JSON", // WebService returns the JSON type
URL: webserviceurl + "webservice1.asmx/helloworld", // address and method name combination for WebService call ---- wsurl/method name
Data: "{}", // here is the parameter to be passed, in the format of data: "{paraname: paravalue}", as shown below
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: 'wishful thinking ', value2: 'Good luck', value3: 'niu Niu ', value4: 2009 }",
Datatype: 'json ',
Success: function (result ){
$ ('# Dictionary'). append (result. D );
}
});
});
});
// Return the Set (referenced from the network, which is very helpful)
$ (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 the composite 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', // The returned type is XML, which is different from the preceding JSON.
Success: function (result ){
// Demonstrate capture
Try {
$ (Result). Find ("Table1"). Each (function (){
$ ('# Dictionary '). append ($ (this ). find ("ID "). text () + "" + $ (this ). find ("value "). text ());
});
}
Catch (e ){
Alert (E );
Return;
}
},
Error: function (result, status) {// if no above capture error occurs, the callback function here will be executed
If (status = 'error '){
Alert (Status );
}
}
});
});
});
// Ajax provides feedback to users, and uses the ajaxstart and ajaxstop methods to demonstrate Ajax callback for tracking related events, the two methods can be added to the jquery object for callback before and after Ajax
// but the monitoring on AJAX itself is global
$ (document ). ready (function () {
$ ('# loading '). ajaxstart (function () {
$ (this ). show ();
}). ajaxstop (function () {
$ (this ). hide ();
});