Ajax gets JSON data passed in the background

Source: Internet
Author: User

 

When using the Ajax method of jquery recently, the data that needs to be returned is JSON data, and data processing in success returns different ways to generate JSON data depending on how it is returned. How it should be handled in the $.ajax method is simply explained.

First, give the JSON data to be transmitted: [{"Demodata": "This is the JSON"}]

1, use the normal ASPX page to handle

JS Code
  1. $.ajax ({
  2. Type: "POST",
  3. URL: "default.aspx",
  4. DataType: "JSON",
  5. Success: function (data) {
  6. $ ("Input#showtime"). Val (Data[0].demodata);
  7. },
  8. Error: function (XMLHttpRequest, textstatus, Errorthrown) {
  9. alert (Errorthrown);
  10. }
  11. });

Here is the code that passes the data in the background

Java Code
    1. Response.Clear ();
    2. Response.Write ("[{\" demodata\ ": \" This is the JSON data\ "}]");
    3. Response.Flush ();
    4. Response.End ();

This process of processing the data passed directly into the JSON data, that is to say, the foreground JS code can directly parse the data into JSON object data, rather than string data, such as Data[0].demodata, this is the direct use of this JSON object data

2, using WebService (ASMX) to process

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

JS Code
  1. $.ajax ({
  2. Type: "POST",
  3. URL: "Jquerycsmethodform.asmx/getdemodata",
  4. DataType: "json",/* This sentence can not be used, no impact * /
  5. ContentType: "Application/json; Charset=utf-8 ",
  6. Success: function (data) {
  7. $ ("Input#showtime"). Val (eval ('(' + DATA.D + ') ') [0].demodata];
  8. //There are two ways to convert data, and the effect of two processing methods is the same
  9. //$ ("Input#showtime"). Val (eval (DATA.D) [0].demodata];
  10. },
  11. Error: function (XMLHttpRequest, textstatus, Errorthrown) {
  12. alert (Errorthrown);
  13. }
  14. });

Here is the method code for ASMX

Java Code
    1. Public static String Getdemodata () {
    2. return "[{\" demodata\ ": \" This is the JSON data\ "}]";
    3. }

The processing here treats the JSON data that is passed back as a string, where the data is processed by eval to be the real JSON object data.

That

Java Code
    1. Success:function (data) {
    2. eval (data);
    3. }

Ajax gets JSON data passed in the background

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.