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)