我的Ajax调用成功块没有被调用。我在这里附加我的代码,你能帮忙吗? JS代码:-
$(document).ready(function(){
$('.form_table_action').submit(function() {
var userName = document.getElementById("username").value;
var password = document.getElementById("password").value;
$.ajax({
type: 'POST',
url: '/bin/servlet/loginData',
data: {
'username': userName,
'password': password,
},
success: function (data) {
console.log('Login success');
window.location = "http://localhost:4502/content/AEMTraining/en/loginsuccesspage.html?wcmmode=disabled";
},
error: function(jqXHR) {
console.log('Error while login');
},
dataType: 'html'
});
});
}(;
我的Java代码在登录过程中从ajax调用。
@Component(service= Servlet.class,
property={
Constants.SERVICE_DESCRIPTION + "=Login Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_POST,
"sling.servlet.paths="+ "/bin/servlet/loginData"
})
public class LoginServlet extensions SlingAllMethodsServlet {
/**
* doPost method
* @param request
* @param response
* @throws IOException
*/
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
String userName = request.getParameter("username");
String password = request.getParameter("password");
String outputMsg = null;
if((userName.equals("")) || (password.equals(""))){
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
outputMsg = "User name or password field can not my empty";
}
else {
ResourceResolver resourceResolver=request.getResourceResolver();
UserAuthentication authentication = new UserAuthentication();
if (authentication.verifyUser(userName,password,resourceResolver)){
response.setStatus(HttpServletResponse.SC_OK);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
outputMsg = "Welcome ! You have successfully login";
}
else {
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
outputMsg = "User name or password is wrong.";
}
}
response.getWriter().write(outputMsg);
}
因此,我可以登录并返回消息(成功消息(正在我的页面上打印,但无论我在Ajax的成功块中写什么。那不是在召唤。
来自 jquery 升级指南的内容:
从 jQuery.ajax(( 返回的 jqXHR 对象是一个 jQuery 延迟,历史上有三个额外的方法,其名称与成功、错误和完成的参数对象匹配。这经常使那些没有意识到返回的对象应该被视为延迟的对象的人感到困惑。从jQuery 3.0开始,这些方法已被删除。作为替代方法,请使用"完成"、"失败"和"始终"的延迟标准方法,或使用新的"然后"和"捕获"方法来实现承诺/A+ 合规性。
因此,您可以选择使用相同的编码风格以及"完成"和"失败",或者您想使用带有"then"和"catch"的承诺。
源:
https://jquery.com/upgrade-guide/3.0/#breaking-change-special-case-deferred-methods-removed-from-jquery-ajax