this is a ssm project,i can use ajax or modelAndView alone successfully,but when i integrate them,it fails.
Here is my code and runtime picture(i wish you explain how it wroks and so i appreciate you):
jsp code:
div class="div_style"
input type="button" id="loginWithAjax" onclick="loginWithAjax()"
value="login_with_ajax"
/div
script type="application/javascript"
function loginWithAjax() {
var loginData = {"userAccount": "10001", "userPassword": "123456"};
$.ajax({
type: "post",
url: "<%=basePath%>/user/checkLoginInfo2.do",
contentType: "application/json",
async: false,
dataType: "json",
data: JSON.stringify(loginData),
success: function (data) {
if (data == "SUCCESS") {
alert("ajax login succeed...");
} else {
alert("user account pwd error");
}
},
error: function () {
alter("loginWithAjax-error:function()");
}
});
}
/script
Controller code:
@RequestMapping("checkLoginInfo2.do")
public String checkLoginInfo2(@RequestBody String json) {
System.out.println(json);
// ModelAndView modelAndView = new ModelAndView();
JSONObject jsonObject = JSONObject.parseObject(json);
System.out.println(jsonObject.get("userAccount").equals("10001"));
System.out.println(jsonObject.get("userPassword").equals("123456"));
if (jsonObject.get("userAccount").equals("10001") && jsonObject.get("userPassword").equals("123456")) {
System.out.println(11111);
// modelAndView.setViewName("main");
// return modelAndView;
return "main";
} else {
System.out.println(22222);
// modelAndView.setViewName("error/404");
// return modelAndView;
return "error/404";
}
}
Here is the picture:
enter image description here