JQuery-based ajax function for web service json Conversion

Source: Internet
Author: User

However, I really don't know how to solve the question in this article. If you miss this thing because of this poor question, it is a pity.

I have referenced many articles before doing this:

Http://www.roseindia.net/tutorials/json/parse-message-JSON-JS.shtml

Http://www.json.org/js.html

Http://funkatron.com/site/comments/safely-parsing-json-in-javascript/

Http://docs.jquery.com/Ajax/jQuery.getJSON

Http://encosia.com/2009/06/29/never-worry-about-asp-net-ajaxs-d-again/

Http://encosia.com/2009/04/07/using-complex-types-to-make-calling-services-less-complex/

Http://www.isolutionteam.co.uk/json-jquery-ajax-aspnet-and-c-to-get-ajaxed-data-table-rows-passing-multiple-parameters/

The first step is to convert a able to the desired format on the Server:

Originally intended to use. net javascriptSerializer (combined with the List of the Genirics Library), but later found that NewtonSoft is used. json is more flexible and highly scalable. Some code can be found on the Internet.

First, let's take a look at how I implement Data conversion on the Server:

Here, not only Datatable and DataSet can be converted, but also the JavascriptSerializer conversion function will be extended later. Note that the final result of conversion must be a [] character string. at first, I tried to use {} to finally return the json as an object. The result cannot be converted without a clue on the client. At last, I saw an article introduction. the results returned by your web Services in. net 3.5 are automatically packaged into a json object named d, for example, {"d ":***}. therefore, I only need to generate an array package on the server side.

Then I created a database table with the following content:

,

The web method is defined as follows:

The Serializer method has been packaged in DNA _ JSON. It is extended based on the Basic conversion class of NewtonSoft. Json.

The second step is about the client:

First, we need to use several js, jQuery. js, and json2.js.

As shown in the figure above, ajax and JSON of jQuery are used. Parse () Conversion Function.

Note that res. d is the json property that is packaged for you by default in. net 3.5. If you use framework2.0.

Finally, let's look at how to display these ojbect arrays in a table. The Code is as follows:

Copy codeThe Code is as follows: <script type = "text/javascript"> <! --
Function BuildTable (msg ){
If ($ ('# tblresult'). length! = 0) // remove table if it exists
{$ ("# TblResult"). remove ();}
Var table = '<table class = "table. display "> <thead> <tr> <th> First Name </th> <th> Last Name </th> <th> Email </th> </thead> <tbody> ';
For (var I = 0; I <= msg. length-1; I ++ ){
Var row = '<tr> ';
Row + = '<td>' + msg [I]. UserName + '</td> ';
Row + = '<td>' + msg [I]. FirstName + '</td> ';
Row + = '<td>' + msg [I]. email + '</td> ';
Row + = '</tr> ';
Table + = row;
}
Table + = '</tbody> </table> ';
Parameters ('{example1'{.html (table );
}
// --> </Script>

If you still need source code, leave a message. Next, I will try to use this method to introduce some jQuery ajax plug-ins to you, and then encapsulate some datagrid plug-ins into asp.net controls, so that you do not need to use Microsoft's bulky datagrid.

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.