Use jquery Ajax to access WebService

Source: Internet
Author: User

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
}

}

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.