これは私の問題です.Ajaxはすごくうまく動作していますが、何らかの理由ですべてのデータを処理していますが、現時点で無視しているローダーと成功divは表示されません。AJAX Postローダとメッセージが表示されない
$(document).ready(function(){
$('#msform').submit(function(event){
event.preventDefault();
apfaddpost();
})
})
function apfaddpost() {
var fd = new FormData($('#msform')[0]);
fd.append("main_image", $('#main_image')[0].files[0]);
fd.append("action", 'apf_addpost');
//Append here your necessary data
jQuery.ajax({
type: 'POST',
url: apfajax.ajaxurl,
data: fd,
processData: false,
contentType: false,
beforeSend: function(){
//here should be the issue. I'm hiding the form and showing the loader div
$('#msform').hide();
$('#processing').show();
},
success: function(data, textStatus, XMLHttpRequest) {
//when it's completed hide loader and show success message
$('#processing').hide();
$('#apf-response').show();
var id = '#success';
jQuery(id).html('');
jQuery(id).append(data);
resetvalues($('#msform'));
},
error: function(MLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
これが正しい方法です:
マイHTMLは
<form id="msform" action="#">
<fieldset>
<!-- some fields -->
<input type="submit" Value="submit">
</fieldset>
</form>
<div id="processing" class="row" style="display:none"></div>
<div id="apf-response" class="row" style="display:none"></div>
私のjQueryの/ AJAXのようなものは次のように見ているのですか?私は間違っているの?
EDIT は、この問題がevent.preventDefault();
かaction="#"
によって与えられることができることがその可能ですか?
が
を解決し、私はそれが働い
function apfaddpost() {
var fd = new FormData($('#msform')[0]);
fd.append("main_image", $('#main_image')[0].files[0]);
fd.append("action", 'apf_addpost');
$('#form-container').hide();
$('#processing').show();
var postProject = $.ajax({
type: 'POST',
url: apfajax.ajaxurl,
data: fd,
processData: false,
contentType: false,
});
postProject.done(function(data, textStatus, XMLHttpRequest) {
$('#processing').hide();
$('#apf-response').show();
var id = '#success';
jQuery(id).html('');
jQuery(id).append(data);
resetvalues($('#msform'));
});
postProject.fail(function(MLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
});
}
に私のjQueryのコードを変更しました!
に動作します$( "#処理")を配置しようショー(); Ajaxリクエストの開始前。 –
@DharaParmar残念ながら、それを置くことができません – XiLab
AJAX呼び出しが行われたときに成功ハンドラが起動されていることを確認できますか?また、 '.done()'や '.fail()'のような約束を使って、AJAXオブジェクトにネストされた 'success'や' error'ハンドラを使うのではなく、実際には簡単です。 – Terry