jqueryでフォームを処理するための汎用関数を作成しようとしています。レスポンスを残りのAPIから400とすると、エラー句の中でフォームオブジェクトにアクセスできません。コードe.targetを以下にJquery Ajaxエラー・エラー・ブロックでフォーム・オブジェクトを取得できません
は
$(document).ready(function(){
$('form').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
alert("Unexpected , contact admin");// handle the invalid form...
} else {
event.preventDefault();
$.ajax({
url: e.target.action,
method: e.target.method,
data:$(this).serialize(),
success: function(data) {
alert(JSON.stringify(data));
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
if (XMLHttpRequest.status == 0) {
alert(' Check Your Network.');
}else if (XMLHttpRequest.status == 404) {
alert('Requested URL not found.');
}else if (XMLHttpRequest.status == 500) {
alert('Internel Server Error.');
}else if (XMLHttpRequest.status == 400) {// Bad Request
$(e.target).append('<div class="alert alert-danger fade in alert-dismissable"><a href="#" class="close" data-dismiss="alert" aria-label="close" title="close">×</a><strong>Danger!</strong> This alert box indicates a dangerous or potentially negative action.</div>')
}else {
alert('Unknow Error.\n' + XMLHttpRequest.responseText);
}
}
});
return false;
}
});
})
私が得たエラーが動作しない(良い方法ではない)、フォームオブジェクトですが、でも、グローバル変数を使用して、エラー句で電子にアクセスすることはできません$(e.tartget)のエラーブロックは、eは定義されていません。
経由しても問題 – charlietfl