ajax は非同期呼び出しであるため、結果を待つのではなく、そこに書かれている次の行にジャンプします。したがって、あなたの場合、submitメソッドで ajax を呼び出すと、次のようになります。
var xhr = $.ajax({}); // here you assign the ajax to a var xhr
その呼び出しの直後に、次のように状態をコンソールに出力していました。
console.log(xhr.readyState); // logs the state of the ajax
したがって、あなたの場合、問題はajaxが非同期呼び出しであるためxhr、その直後にajaxコードが割り当てられるたびに、状態をログに記録しています。そのため、毎回ログに記録されます1。
ドキュメントを読むと、コールバックがあることがわかりcomplete:fnます。それを使用できます。
$('.delsub').on('submit', function (event){
event.preventDefault();
var xhr = $.ajax({
url: 'delRept.php',
type: 'POST',
data: "&delsub=delsub",
success: function(d){
console.log(d);
},
complete:function(xhr){
console.log(xhr.readyState);
console.log(xhr.status);
}
});
});
または、さまざまなステータスでいくつかの操作を実行する場合は、次を使用できますstatusCode:{}。
$('.delsub').on('submit', function (event){
event.preventDefault();
var xhr = $.ajax({
url: 'delRept.php',
type: 'POST',
data: "&delsub=delsub",
statusCode:{
200:function(){
console.log(xhr.readyState);
console.log(xhr.status);
alert('200');
},
304:function(){
console.log(xhr.readyState);
console.log(xhr.status);
alert('304');
}
},
success: function(d){
console.log(d);
}
});
});