Ajax in Javascript implements foreground-to-background interaction

Source: Internet
Author: User

First case: The foreground incoming string parameter returns a JSON string, or a JSON array

The code is as follows :

Front desk:

$.ajax ({          URL: "Xxx/xxx.action",          data: "Id=xxx",          Cache:false,          async:false,          success:function ( Result)      {        A: The array returned by the receive backend is               var data = eval (result);        if (typeof (data) = "undefined" && data.length > 0)         {for        (var i=0;i<data.length;i++)        {        data[i]["id"];        data[i]["name"];        }        } B: The background returns the string, then the direct receive      },        error:function () {            alert ("Table loading error ...");        }      );
Background:
PrintWriter out = Servletactioncontext.getresponse (). getwriter ();/* Back JSON string * /
              




Out.print (Jarray); Out.flush ();
return null;

Second case: The foreground passes in the JSON array as parameters to interact with the background
Front desk:
function Delcheckeddata () {var array = new Array ();                 $ (": Checkbox[name= ' chk '][checked=true]"). each (function () {Array.push ({ID: $ (this). Val ()            }); }) if (Array.Length > 0) {var Gridjson = json.stringify (array, function (key, value) {Retu        RN value;        });        $.ajax ({url: "/xxx/xxx.action", Cache:false, Data: "idlistjson=" + Gridjson, Async:false,        Success:function (Result) {var instdata = eval (result);        corresponding processing}, Error:function () {alert ("error ...");            }        }); }} 
background:
Jsonarray Instarray = jsonarray.fromobject ("parameter");        for (int i = 0; i < instarray.size (); i++)        {            Jsonobject instj = (jsonobject) instarray.get (i);            String sId = instj.get ("id"). toString ();            Get the ID to do the appropriate action        }

Ajax in Javascript implements foreground-to-background interaction

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.