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

How to deal with the json data sent by success in Ajax background

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to deal with the json data sent by Ajax background success. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Recently, when using the ajax method of JQuery, the data that needs to be returned is json data. In the success return, the data processing will generate json data in different ways depending on the return method. How it should be handled in the $.ajax method is briefly explained.

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

1. Use a normal aspx page to handle

$.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 the 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

In this way, the passed data is not treated 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 for asmx

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.

That is,

Success:function (data) {eval (data);} this is the end of the article on "how to deal with json data sent from Ajax background success". I hope the above content can be helpful to you so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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