最近在写程序时,都是使用ajax来传递数据的。今天在写程序时,出现了既要传递数据,又要跳转页面的情况。虽然一般遇到这种情况后,我们可不再使用ajax来操作。不过,今天,心血来潮想试着用ajax来实现。
因为ajax是无跳转功能的,所以,在Controller层使用return new ModelAndView()肯定是行不通的,所以我们唯一可以做手脚的地方应该是当ajax返回成功数据时,来做处理。于是,我就尝试了在success的返回方法中,使用了window.location.href,结果成功了。具体代码如下所示
<script type="text/javascript">
function onClick1() {
var pwd1 = document.getElementById("password1").value;
var pwd2 = document.getElementById("password2").value;
var userName=document.getElementById("username").value;
if (pwd1 == pwd2) {
var user = {
username : userName,
password : pwd1
};
$.ajax({
url : "../user/adduserReturn",
type : "post",
data : user,
async : false,
success : function(data) {
window.location.href="test";
}
});
} else {
alert("您输入的两次密码不一致");
}
}
</script>