This article will give you a detailed introduction of jquery Ajax calling methods to access WebService. If you need them, refer to the opposite.
Jquery code
The Code is as follows: |
Copy code |
Jquery. ajax ({
Type: 'post ',
Datatype: 'json ',
Success: function (data ){
// Do something...
},
Error: function (error ){
// Do something...
}
});
|
WebService Processing
[WebMethod]
The Code is as follows: |
Copy code |
Public string Hello (){
// Return json
// Method 1: manual stitching
Return "{" Test ":" Test-Value "," Test ":" Test-Value "}";
// Method 2: serialize json
// Introduce System. Runtime. Serialization. Json. DataContractJsonSerializer
// Serialize data to json using the WriteObject Method
// Use the JsonConvert. SerializeObject of Newtonsoft. Json. dll for processing.
// Method 3: spit the directly spliced json string into the browser and do not need to return
System. Web. HttpContext. Current. Response. Clear (); System. Web. HttpContext. Current. Response. ContentType = "application/json "; System. Web. HttpContext. Current. Response. Expires = 0; System. Web. HttpContext. Current. Response. Cache. SetNoStore (); System. Web. HttpContext. Current. Response. Write (jsonContent );
System. Web. HttpContext. Current. Response. End ();
}
|
This is done, but some friends said that the returned xml data was not found url problems,
The Code is as follows: |
Copy code |
Page_Load (object sender, EventArgs e) { String method = Request. QueryString ["method"]; If ("Hello". Equals (method )) { // Copy the WebService method or directly call the WebService Method }
}
|