问题
I have a basic AJAX request like so:
$.ajax({
url: 'check_password.php',
type: 'post',
data: 'password='+password,
dataType: 'json',
success: function(json) {
if (json['success']) {
//Continue with default action
}else{
//Password is invalid, stop the script and return an error
preventDefault();
$(".error").show();
}
}
回答1:
I'd suggest instead preventing the default completely, then submitting the form (if it is a form) by using .submit()
on the form node, thus bypassing the jQuery event handler.
$("#myform").on("submit",function(event){
event.preventDefault();
var form = this;
$.ajax({
url: theurl,
type: "POST",
data: thedata
dataType: "json"
}).done(function(json){
if (json.success) {
form.submit();
}
else {
$("#error").show();
}
});
});
来源:https://stackoverflow.com/questions/15033991/perform-ajax-request-before-continuing-with-default-action