Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the processing methods when ajax returns JSON in JQuery?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "what are the processing methods when ajax returns JSON in JQuery", the content is simple and clear, and I hope it can help you solve your doubts, so let me lead you to study and learn about "what is the way to deal with ajax returning JSON in JQuery".

First, some basic knowledge of JSON.

Objects in JSON are identified by "{}", a "{}" represents an object, such as {"AreaId": "123"}, and the value of the object is in the form of key-value pairs (key:value).

"[]", identifies the array, and splits the data within the array by ",", such as ["AreaId": "123"," AreaId ":" 345"].

In many cases, it's an array of objects, and that's what it is:

[{"AreaId": "123"}, {" AreaId ":" 345"}]

In fact, an array is also an object, and the above format can also be written as follows:

{"Area": [{"AreaId": "123"}, {" AreaId ":" 345"}]}

This represents an Area object that has two children, each of which is also an object, and each of which is an AreaId.

The definition format of strings and characters in JSON is similar to that of general C-like language definitions, double quotation marks define strings and single quotation marks define characters.

JSON's Key is enclosed in double quotes, such as "Area" and "AreaId" above, which are enclosed in double quotes. When constructing JSON strings in some languages, escape characters can be used to escape double quotation marks.

First, give the json data to be transmitted: [{"demoData": "This Is The JSON Data"}]

1. Use a normal aspx page to handle

I think this is the easiest way to deal with it. Take a look at the following code

$.ajax ({type: "post", url: "Default.aspx", dataType: "json", success: function (data) {$("input#showTime") .val (data [0] .demoData);}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown);}})

Here is the code for passing data in the background.

Response.Clear (); Response.Write ("[{\" demoData\ ":\" This Is The JSON Data\ "}]"); Response.Flush (); Response.End ()

In this way, the passed data is parsed directly into json data, that is, the foreground js code here may directly parse this data into json object data instead of string data, such as data [0] .demoData

This json object data is directly used here.

2. Using webservice (asmx) to handle this processing will not treat the passed data as json object data, but as a string, as shown in the following code

$.ajax ({type: "post", url: "JqueryCSMethodForm.asmx/GetDemoData", dataType: "json", / * this sentence is available or not, it does not affect * / contentType: "application/json; charset=utf-8", success: function (data) {$("input#showTime") .val (eval ('+ data.d +')') [0] .demoData) / / there are two ways to convert data. The effect of the two processing methods is the same / / $("input#showTime") .val (eval (data.d) [0] .demoData);}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown);}})

Here is the method code of asmx [WebMethod]

Public static string GetDemoData () {return "[{\" demoData\ ":\" This Is The JSON Data\ "}]";}

In this way, the json data passed back is treated as a string, and the data is processed by eval, so that it can become the real json object data.

3. Using ashx files to deal with this is the same as ordinary aspx page processing, so there is no more explanation here!

The above is all the contents of this article entitled "what is the way to deal with ajax when JSON is returned in JQuery?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report