问题是在登录页面上,在提交了不正确的条目后,ajax 请求在按下提交时不会第二次通过。
我已经通过 StackExchange 寻找我的问题的答案,并查看了诸如为什么我的 jQuery 表单不会重新提交之类的链接?以及jQuery 和 AJAX 登录表单等等。看起来我需要取消绑定提交的变量,但不知道该怎么做。
HTML
<div data-role="page" id="login">
<div data-role="header" data-position="inline" data-theme="b">
<h1>My login</h1>
</div><!-- /header -->
<div data-role="content">
<form method="post" id="loginform">
<label for="username">Username:</label> <input type="text" name="username" id="username" value="" />
<label for="passwd">Password:</label> <input type="password" name="passwd" id="passwd" value="" />
<input type="submit" value="Login" />
</form>
</div><!-- /content -->
</div><!-- /login page -->
JavaScript:
$(document).ready(function() {
$("#loginform").on('submit',function(event){
var $form = $(this),
serializedData = $form.serialize();
// fire off the request to /form.py
$.ajax({
url: "https://mywebsite/cgi-bin/form.py",
type: "post",
data: serializedData,
// callback handler that will be called on success
success: function(response, textStatus, jqXHR){
console.log(response);
// If login is unsuccessful, log message and return to login screen again
if (response == "INVALID_USER\n") {
alert("sorry, try again");
} else {
// Login successful - do something
}
},
// callback handler that will be called on error
error: function(jqXHR, textStatus, errorThrown){
console.log(
"The following error occured: "+
textStatus, errorThrown);
},
// callback handler that will be called on completion
// which means, either on success or error
complete: function(){
//----- DO I NEED TO UNBIND SOMETHING HERE?
var dummy = 1;
}
}); // end of ajax call
}); // end of submit handler
});