Ajax back-and-forth interaction returns both normal and JSON formats __ajax

Source: Internet
Author: User

using Ali Fastjson below is Pom.xml

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactid>fastjson</ artifactid>
            <version>1.2.9</version>
        </dependency>

Ajax return String

    JS foreground ajax note introduces jquery files such as jquery.min.js function Testajax () {//text value var uname = $ ("#username"). VA
        L ();
        var pwd = $ ("#password"). Val (); Alert (PWD) $.ajax ({type: ' post ',//Submit as URL: "...) /returnstring.do ",//path//Parameter data: {username:uname, password : pwd}, Cache:false,//return to normal character streams do not write DataType: "JSON" success:functio
            N (data) {alert (data);
    }
        }); //html code I bind is focus lost event so I added a test box <form action= "#" id= "MyForm" > Name: <input type= "text" id= "Usernam" E "name=" username "/><br/> Password: <input type=" text "name=" password "id=" password "onblur=" Testajax "() "/> <br/> Test box: <input type=" text "/>" <br/> ${pagecontext.request.contextpath} </form >//Background code is just functional testing without writing the actual content @RequestMapping ("/returnsTring ")//This is the Spring framework annotation public void returnstring (String username,string password,httpservletresponse response) {
        System.out.println (Username+password);
        try {//write out Object Stream Response.getwriter (). println ("test string");
        catch (IOException e) {e.printstacktrace (); }
    }

Ajax return to JSON format

    Foreground Ajax
Function returnjsonlist () {
        $.ajax ({
            type: ' Post ',
            URL: ' ... /returnjsonlist.do ',
            dataType: "JSON",
            success:function (data) {
                alert (data);
                The number of I loops  value Object Id,name is a property
                $.each (data, function (I, value) {                          
                            $ ("#remark"). Append (
                             "<tr ><td> "+ value.id +" </td><td> "
                                    + value.name +" </td><td> "+ value.t
                                    +" </ Td><td> "+ value.x + </td></tr>");} 
                );}
    
//html code Test button Click Time then return to the collection stitching to the table <input type= "button" name= "Test return to JSON format list collection" onclick= "Returnjsonlist ()"/> <table class= "Table table-striped" id= "remark" > <tr> <TD&G T; School number </td> <td> name </td> <td> date </td> <td> age </td>
        ; </tr> </table> 
Background code @RequestMapping ("/returnjsonlist") public void Testprco (HttpServletResponse response) {System.out.print
        ln ("OK returnjsonlist");
            try {list<demo> List = new arraylist<> ();
            Demo D1 = new Demo (1, "Test", New Date ());
            Demo D2 = new Demo (2, "Test", New Date ());
            Demo D3 = new Demo (3, "Test", New Date ());
            Date conversions are added @jsonfield (format= "Yyyy-mm-dd") List.add (D1) on entity object attributes;
            List.add (D2);
            List.add (D3);
            /*map<integer, string> Map = new hashmap<> ();
            Map.put (1, "test01");
            Map.put (2, "test02");
            Map.put (3, "test03");//simple rough, for map This sentence also applies String json = json.tojsonstring (list);
            SYSTEM.OUT.PRINTLN (JSON);
        Get flow to JSP pass Data Response.getwriter (). println (JSON);
        catch (IOException e) {e.printstacktrace ();
 }
    }

attached
JSON format Conversion

public static void Main (string[] args) {
             list<object> List = new arraylist<> ();
             List.add ("test");
             List.add ("Test 2");
             List.add ("Test 3");
             JSON format Conversion  map strings are adapted to
             string str = json.tojsonstring (list);
             System.out.println (str);
    

Results

[Test, Test 2, Test 3]

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.