Home > Web Front-end > JS Tutorial > Implement json conversion of web service based on jQuery's ajax function_jquery

Implement json conversion of web service based on jQuery's ajax function_jquery

WBOY
Release: 2016-05-16 18:47:22
Original
1446 people have browsed it

But I really don’t know how to come up with the title of this article. It would be a pity if you missed this thing because of this poor title.

I referred to many articles before making this thing:

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 you need to do is how to convert a datatable to the format you need on the server side:

Originally I wanted to use .net's javascriptSerializer to complete it (combined with Genirics library's List, etc.), but later I found that using NewtonSoft.Json is more flexible and has better scalability. I even found some codes on the Internet that can be used ready-made.

Let’s take a look at how I implemented Data conversion on the server side:

Not only Datatable but also DataSet can be converted here, and the conversion function of JavascriptSerializer will be expanded in the future. It should be noted that the final result of the conversion needs to be a string wrapped in []. At first, I tried to use {} to finally return json as an object. As a result, the client really had no clue and could not convert at all. Finally, I saw an article introducing that in .net 3.5, the results returned by your web services will be automatically packaged into a json object with the name d, such as: {"d":***}. So I only have The server side generates a package in array form.

Then I created a database table with the following content:

,

The web methods defined by

are as follows:

Serializer method has been packaged in DNA_JSON. It is extended based on NewtonSoft.Json's basic conversion class.

The second step is the client stuff:

First we need to use a few js, jQuery.js, json2.js is OK.

As shown in the picture above, we use jQuery’s ajax and JSON. Parse() conversion function.

It is also important to note that res.d is the attribute of .net 3.5’s packaged json by default. If you use framework2.0, you won't have this problem.

Finally, let’s see how to display the array composed of these objects in a table. The code is as follows:

Copy code The code is as follows:



If you still need the source code, please leave a message. Next, I will try to use this method to introduce some jQuery ajax plug-ins to everyone, and then encapsulate some datagrid plug-ins into asp.net controls, so that you do not need to use Microsoft's bulky datagrid.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template