jquery Ajax Call Access WebService

Source: Internet
Author: User
Tags httpcontext serialization


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: Hand stitching

Return "{" Test ":" Test-value "," Testing ":" Test-value "}";

Method 2: Serialization of JSON

Introduction of System.Runtime.Serialization.Json.DataContractJsonSerializer

Serializing into JSON using the WriteObject method

Or use Newtonsoft.Json.dll's jsonconvert.serializeobject to deal with it.

Method 3: Spit directly into the JSON string to the browser, 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 achieved, but some friends say that the return of the XML data, later found the URL problem,

  code is as follows copy code

Page_Load ( Object sender, EventArgs e)    
{   
    String method = Request.qu Erystring["Method"];   
    if ("Hello"). Equals (method))    
    {   
        //Copy the WebService method or directly call WebService    
   }    
  
}   
  

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.