在第A页上,我使用ajax将数据发送到服务器。在服务器端,在spring控制器获取数据后,它返回欢迎页面B. Evergthing在firefox和IE上正常工作。但是在chrome上,在ajax调用成功向服务器发送数据之后,我们可以获得响应:我想要的页面B.但是页面B只显示1秒钟。然后再次跳回到页面A.现在想到为什么?感谢。
表单html:
<form class="form" id="register-form">
<input id="username" type="text" placeholder="Username" name="username">
<input id="password1" type="password" placeholder="Password" name="password1" >
<input id="password2" type="password" placeholder="Password" name="password2">
<input id="email" type="text" placeholder="Email" name="email">
<input id="phonenumber" type="text" placeholder="Phone Number" name="phonenumber">
<button onclick="register()" id="register-button">Join us!</button>
</form>
的Ajax:
$.ajax({
url: "/myporject/user/addUser",
type: 'GET',
dataType: 'text',
contentType: "application/json; charset=utf-8",
async: false,
cache : false,
data: {
username:username,
password:pwd1,
email:email,
phonenumber:phone
},
success : function(response) {
alert("response:" + response);
document.open();
document.write(response);
document.close();
},
error: function(xhr, textStatus, error){
alert("error!!!");
console.log(xhr.statusText);
alert(textStatus);
console.log(error);
}
});
Spring控制器:
@RequestMapping(value = "/addUser", method = RequestMethod.GET)
public @ResponseBody ModelAndView addUser(
@RequestParam(value = "username") String username,
@RequestParam(value = "password") String password,
@RequestParam(value = "email") String email,
@RequestParam(value = "phonenumber") String phonenumber) {
User user = userService.createUser(username, password, email, phonenumber,
User.ROLE_CUSTOMER);
ModelAndView myview = new ModelAndView("welcome");
return myview;
}
答案 0 :(得分:0)
添加type =“button”(例如<button type="button" ...>
),以便(标准的,非Ajax)表单提交不会同时发生。
或者使用jQuery绑定click处理程序并使用event.preventDefault()