Processing Method When JQuery ajax returns JSON (three methods), jqueryjson

Source: Internet
Author: User

Processing Method When JQuery ajax returns JSON (three methods), jqueryjson

Recently, when using JQuery's ajax method, the returned data is required to be json data. The following problems are encountered during the processing process: when using different methods to generate json data, at $. the following describes how to process the ajax method. Because I am using asp.net, all pages are processed. net! Other methods should be the same.

I. Basic knowledge about JSON.

Objects in JSON are identified by "{}". A "{}" represents an object, for example, {"AreaId": "123 "}, the object value is in the form of a key-value Pair (key: value ).

"[]" Identifies the array. Each data in the array is separated by ",", for example, ["AreaId": "123", "AreaId": "345"].

In many cases, the object array is like this:

[{“AreaId”:”123”},{“AreaId”:”345”}]

In fact, arrays are also an object. The above format can also be written as follows:

{“Area”:[{“AreaId”:”123”},{“AreaId”:”345”}]}

This indicates an Area object. It has two sub-data, each of which is also an object, and each sub-object is AreaId.

The definition formats of strings and characters in JSON are similar to General C language definitions. Double quotation marks define strings and single quotation marks define characters.

JSON keys are enclosed in double quotation marks. For example, the preceding "Area" and "AreaId" are enclosed in double quotation marks. When constructing JSON strings in some languages, escape double quotation marks with escape characters.

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

1. Use a normal aspx page for processing

I think this method is the easiest to handle. Let's 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(); 

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. Using webservice (asmx) to process this processing method will not treat the transmitted data as json object data.Is processed as a string, 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 [WebMethod].

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

The processing method here 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,

3. Use the ashx file to process this method, which is the same as the normal aspx page processing, so I will not explain it more here!

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.