Use jquery to access WebService and return JSON code 1th/3 Page _ Practical Tips

Source: Internet
Author: User
Tags serialization
This is generally true in our applications, using jquery as the client framework, and AJAX requests often return HTML or JSON. The HTML is not discussed here. Returning JSON is typically a handler.ashx to handle requests, piecing together strings to return JSON. The WS is thus discarded because WS returns XML and is inconvenient to use.
So I think the perfect solution is to get WS to return JSON and not asp.net the AJAX client box is the ideal solution.
Why did WebService return to JSON when they found that the ASP.net AJAX client framework request WebService returns JSON? Capture analysis to the key in the request headers "Content-type:application/json;utf-8", so WebService use JSON serialization, should be " System.Web.Script.Serialization.JavaScriptSerializer "This class completes the work through the Web.config configuration, putting *. The ASMX was handed over to System.Web.Extensions.Dll. That is, here or using the ASP.net ajax, but the service side of the section, I am here directly with the ASP.net 3.5
The above is long-winded, the concrete method is very simple, see example
Ws1.asmx
Copy Code code as follows:

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Linq;
Using System.Web;
Using System.Web.Services;
Using System.Web.Services.Protocols;
Using System.Xml.Linq;

Namespace Test2
{
<summary>
Summary description for WS1
</summary>
[WebService (Namespace = "http://onewww.org/")]
[WebServiceBinding (ConformsTo = wsiprofiles.basicprofile1_1)]
[ToolboxItem (False)]

[System.Web.Script.Services.ScriptService]
public class WS1:System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld ()
{
Return to "Hello World";
}

[WebMethod]
Public TestUser CreateUser (String Name,int age)
{
return new TestUser {name = name, age = age};
}
}

public class TestUser
{
public string Name {get; set;}
public int Age {get; set;}
}
}

Current 1/3 page 123 Next read the full text

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.