Spring MVC front-end data interaction notes (troubleshooting 415,400 issues)

Source: Internet
Author: User

1: Front-end send data, backend return interfaceA: Browser
$.ajax ({    "/rest/usercontroller/login",    "POST",    data: {"username": " Chenhao "," Password ":" 123456 "}});

B: Service Side
@RequestMapping ("/login") public String  login (@Valid user user) {   if  (userservice.login (user)) {        return "index";   }    return "Login";}
Note: The parameter user type for the backend can also be a map type, but it is best to use a custom entity class, which can be verified by the annotation @valid for the user entity class. 2: Front end sends data, back end returns JSON data (fetch data from Requestparameter)A: Browser
$.ajax ({    "/rest/usercontroller/test",    "POST"    , "JSON",    data: { "A": "B"});
B: Server-side
@RequestMapping ("/test") @ResponseBodypublic  map<string, object> test (map<string, object> user) {    Mapnew hashmap<string, object>();    Map.put ("user", user);     return map;}
3: Front end sends data, back end returns JSON data (fetch data from requestbody)A: Browser
$.ajax ({    "/rest/usercontroller/test",    "POST"    , "JSON"    , " Application/json ",    data:JSON.stringify ({" a ":" B "})});

B: Service Side
@RequestMapping ("/test") @ResponseBodypublic  map<string, object> Test (@ Requestbody map<string, object> user) {    Mapnew hashmap<string, object>() ;    Map.put ("user", user);     return map;}
4: Back-end data receive attention pointsThe configuration file for Spring-mvc needs to be configured as (a jar package that requires the corresponding JSON): Mappingjacksonhttpmessageconverter and Mappingjackson2httpmessageconverter correspond to a different Jackson bag
<!---<class= " Org.springframework.http.converter.json.MappingJackson2HttpMessageConverter ">    < Value = "Application/json"  name= "Supportedmediatypes"  /> </ Bean >

Spring MVC front-end data interaction notes (troubleshooting 415,400 issues)

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.