平台:春天 我正在使用ajax调用相同的函数,首先是模糊函数,然后是单击函数。 但是模糊功能正在工作,而点击功能却没有。我更改了我调用的函数的名称,然后它正常工作。
请帮我找一个解决方案。我想在模糊和点击功能上向函数'isValidPassword'发送ajax调用。
在点击和模糊时,我必须执行以下代码。
if (oldPassword != "") {
$.ajax({
url: 'isValidPassword.html?oldpassword=' + oldPassword,
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data != "Valid") $("#oldpassworderror").html(data);
return false;
}
});
}
编辑:我的问题。我正在尝试在页面中执行“更改密码”。所以首先我必须检查输入的密码是否与保存的密码相同。为此,我正在使用ajax调用。如果输入的密码与旧密码不同,我必须在标签输出(模糊)中显示错误消息,并且当用户单击“保存”(单击)按钮时也应显示相同的消息。但现在ajax调用仅适用于模糊功能,而不是点击。
这是“isValidPassword”功能代码
@RequestMapping(value = "/isValidPassword", method = RequestMethod.GET)
public @ResponseBody
String isValidPassword(@RequestParam("oldpassword") String oldpassword,
User user, HttpSession session ) throws Exception {
String login = (String)session.getAttribute("userName");
user.setLogin(login);
user.setPassword(oldpassword);
String message = null;
// Check whether old password is valid and
// give a message in front end
if (userService.isValidPassword(user)) {
message = "Valid";
} else {
message = "Incorrect old password";
}
return message;
}
答案 0 :(得分:1)
尝试缓存..
if (oldPassword != "") {
$.ajax({
url: 'isValidPassword.html?oldpassword=' + oldPassword,
type: 'GET',
cache: false, // disable caching
contentType: 'application/json; charset=utf-8',
success: function (data) {
if (data != "Valid") $("#oldpassworderror").html(data);
return false;
}
});
}
答案 1 :(得分:0)
要正确测试此代码,您应该以这种方式准备AJAX调用:
$.ajax({
url: 'isValidPassword.html?oldpassword=' + oldPassword,
type: 'GET',
contentType: 'application/json; charset=utf-8',
success: function () {
alert('success');
},
error: function () {
alert('error');
}
});
因此您可以观察问题是在客户端还是服务器端。