Analysis of the json data transmitted by the Ajax backend success, ajaxjson

Source: Internet
Author: User

Analysis of the json data transmitted by the Ajax backend success, ajaxjson

Recently, when using JQuery's ajax method, the data to be returned is json data. In success return, data processing uses different methods to generate json data based on different return methods. In the $. ajax method, how should we handle it.

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

1. Use a normal aspx page for processing

$.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(); 

This processing method directly parses the transmitted data into json data. That is to say, the front-end js code here may directly parse the data into json object data instead of string data, for example, data [0]. demoData: The json object data is directly used here.

2. Use webservice (asmx) for processing

This method does not treat the transmitted data as json object data, but as a string for processing. The following code:

$. Ajax ({type: "post", url: "JqueryCSMethodForm. asmx/GetDemoData ", dataType:" json ",/* This sentence is usable and does not affect */contentType:" application/json; charset = UTF-8 ", success: function (data) {$ ("input # showTime "). val (eval ('+ data. d + ') [0]. demoData); // There are two data conversion methods. The two processing methods have the same effect. // $ ("input # showTime "). val (eval (data. d) [0]. demoData) ;}, error: function (XMLHttpRequest, textStatus, errorThrown) {alert (errorThrown );}});

The following is the asmx method code.

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

This processing method treats the transmitted json data as a string, and the data must be processed eval so that it can become a real json object data.

That is

success:function(data){ eval(data); } 

The above is a question about json data from the Ajax background success introduced by the editor. I hope it will help you. If you have any questions, please leave a message and I will reply to you in time!

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.