Problem Description: Ajax walk Error:function (e) {}
Issue background:
When testing the controller layer, it is possible to try to responseentity<responsemodel> this type of return, but out of curiosity it is not feasible to look at the map return. The result was unexpected, when I returned to the map JSP page always go error:function (e) {}, it is strange, just used responseentity return to be feasible, and map why not? So I looked at the reason Ajax went error:function,
Reason:
1. Background error
2. Background response interruption
3. The return data type is not JSON
But the break point ruled out, the top two reasons, so speculation is likely to be a third reason.
Map back was not converted to JSON format, sure enough
By adding @responsebody to the @requestmapping, you can convert the return map to JSON format.
First record the problem solving method, late at night, and so have time to dig into the source code
Reference:
http://www.iteye.com/problems/88859
https://segmentfault.com/q/1010000004152735?_ea=509054
Prepare to delve into the reference:
http://mybloggers.blog.163.com/blog/static/1003865092010111631741468/
@ResponseBody
1@RequestMapping (value = "/user/login", method =requestmethod.post)2 PublicMap<string, object>userregister (@RequestBody user user) {3map<string, object> map =NewHashmap<string, object>();4User UserInfo =userloginservice.userregister (user);5 System.out.println (User.getnumber ());6 if(UserInfo! =NULL)7Map.put ("code", "0");8 Else9Map.put ("Code", "1");Ten One returnmap; A -}
<script type= "Text/javascript" >$ (document). Ready (function () {$ ("#login_user"). Click (function () {var username= $ ("#login_account"). Val (); var pass= $ ("#login_userPwd"). Val (); var user={number:username, password:pass};//assemble into JSON format$.ajax ({type:"POST", URL:"Http://localhost:8080/iswust2hand/2hand/user/login", data:JSON.stringify (user), ContentType:' Application/json;charset=utf-8 ', DataType:' JSON ', Success:function (data) {if(Data.code = = ' 0 ') {window.location.href= "/iswust2hand/index.jsp"; Alert ("Welcome to use the West Branch platform!" "); }Else{alert ("Password error, please confirm and login again!" "); }}, Error:function (data) {alert ("Error:" +Data.code); } }); }); });</script>
Front-end novice, will point back end, welcome big God Exchange!
Spring MVC returns the map format JSON data