Analysis of the problem of JSON data coming from Ajax backstage success _ajax related

Source: Internet
Author: User
Tags eval

In the recent Ajax approach to jquery, the data that needs to be returned is JSON data, and data processing in the success return differs in different ways to generate the JSON data, depending on how it is returned. In the $.ajax method should be how to deal with, simple to explain.

First gives the JSON data to be passed: [{"Demodata": "This is the JSON data"}]

1, using the 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); 
} 

This is the code that passes the data in the background

Response.Clear (); 
Response.Write ("[{\" demodata\ ": \" is the JSON Data\ "}]"); 
Response.Flush (); 

This way of processing will pass over the data directly parsed into JSON data, that is, the foreground JS code here may be directly to parse the data into JSON object data, rather than string data, such as Data[0].demodata, here directly using this JSON object data

2, use WebService (ASMX) to process

This approach does not take the data passed over as a JSON object data, but rather as a string, as the following code

$.ajax ({ 
type: "Post", 
URL: "Jquerycsmethodform.asmx/getdemodata", 
dataType: "JSON", * * This sentence can not be used, no impact * * 
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 two ways is the same as 
//$ ("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\ '}] '; 
}

The process here treats the JSON data passed back as a string, where the data is being processed in an eval to become the true JSON object data.

That

Success:function (data) { 
eval (data); 

The above is a small set to introduce the Ajax backstage success to the question of JSON data, I hope to help you, if you have any questions welcome to my message Small series will promptly reply to everyone!

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.