SPRINGMVC Interacting with Ajax

Source: Internet
Author: User

1 Springmvc and Ajax interactions1.1 Request string Response JSON

Data sent by the client: key=value&key1=value1

Response back: JSON

support jar package for 1.1.1json

1.1.2 Build Handler processor
1  PackageOrg.guangsoft.controller;2 3 Importjava.util.ArrayList;4 Importjava.util.List;5 6 ImportOrg.guangsoft.pojo.User;7 ImportOrg.springframework.stereotype.Controller;8 ImportOrg.springframework.web.bind.annotation.RequestBody;9 Importorg.springframework.web.bind.annotation.RequestMapping;Ten ImportOrg.springframework.web.bind.annotation.ResponseBody; One  A @Controller -  Public classUserhandler - { the     //Responsebody convert return value to JSON format response to client -     //requestbody convert request data to JSON object -@RequestMapping ("/checkusername") -      Public@ResponseBody list<user>checkusername (user user) +     { - System.out.println (User.getusername ()); +List<user> userlist =NewArraylist<user>(); AUser User0 =NewUser (); atUser0.setusername ("Java"); -User user1 =NewUser (); -User1.setusername ("Javase"); -User user2 =NewUser (); -User2.setusername ("Java ee"); -         //Object Join Collection in Userlist.add (USER0); - Userlist.add (user1); to Userlist.add (user2); +         returnuserlist; -     } the      *@RequestMapping ("/checkusername2") $      Public@ResponseBody list<user>CheckUsername2 (@RequestBody user user)Panax Notoginseng     { - System.out.println (User.getusername ()); theList<user> userlist =NewArraylist<user>(); +User User0 =NewUser (); AUser0.setusername ("Java"); theUser user1 =NewUser (); +User1.setusername ("Javase"); -User user2 =NewUser (); $User2.setusername ("Java ee"); $         //Object Join Collection - Userlist.add (USER0); - Userlist.add (user1); the Userlist.add (user2); -         returnuserlist;Wuyi     } the}
1.1.3 Complete Ajax send request

Note: SPRINGMVC Ajax, in response to json406 errors, uses JSON-related JAR package 2.4 or later.

1 functionSendajax ()2 {3     varUsername = $ ("#username"). Val ();4     varAge = $ ("#age"). Val ();5     vardiv = $ ("#showDiv");6 $.ajax (7     {8URL: "Checkusername.action",9data:{"username": Username, "age": Age},TenType: "POST", OneDataType: "JSON", ASuccessfunction(data) -         { -             varJsons =data; theDiv.html (""); -              for(vari = 0; i < jsons.length; i++) -             { -Div.append ("<div>" +jsons[i].username+ "</div>"); +             }     -         } +     }); A}
1.2 Request JSON response JSON

Data sent by the client: {key:value,key1:value1}

Response back: JSON

1.2.1 Establishing server-side handler

See above

1.2.2 Client has an AJAX request
1 functionsendAjax2 ()2 {3     varUsername = $ ("#username"). Val ();4     varAge = $ ("#age"). Val ();5     vardiv = $ ("#showDiv");6 $.ajax (7     {8URL: "Checkusername2.action",9Type: "POST",TenData: ' {' username ': ' username ', ' age ': ' 4 '} ', OneContentType: "Application/json;charset=utf-8", ADataType: "JSON", -Successfunction(data) -         { the             varJsons =data; -Div.html (""); -              for(vari = 0; i < jsons.length; i++) -             { +Div.append ("<div>" +jsons[i].username+ "</div>"); -             } +         } A     }); at}
2url Jump

Scope of the Model package data request range, only requests are forwarded to get

If redirection becomes a URL parameter.

Return "Forward:url": Request forwarding

Return "Redirect:url" redirect

SPRINGMVC Interacting with Ajax

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.