我正在嘗試將表單提交給Campaign Monitor。他們提供this代碼示例通過Ajax進行POST。爲什麼不使用AJAX提交此表單?
這是我的多步模態代碼。
var next_step = false;
var final_step = false;
$('.next').on('click', function(e){
e.preventDefault();
if (next_step) {
$('#step-1').slideUp(function(){
$('#step-2').slideDown();
$('.next').html('Submit');// Change button text to submit
final_step = true;
});
}
next_step = true;
if (final_step) {
$('#myform').submit(function (e){
alert('submit started'); //This never fires unless I remove the preventDefault();
e.preventDefault();//But if I remove this, the page will refresh
$.getJSON(
this.action + "?callback=?",
$(this).serialize(),
function (data) {
if (data.Status === 400) {
alert('error');
} else {
alert('success');
}
})
});
}
});
在表格的最後一步,我檢查final_step
是否屬實,如果是的話,繼續前進,通過AJAX提交表單。
問題是它只是沒有做任何事情?但是,如果我從$('#myform')
中刪除e.preventDefault();
,它會按照正常的方式發佈表單,並將您重定向到表單網址。
我該如何解決這個問題?
刪除'if(data.Status === 400){'condition並檢查響應存在 – Maxx
爲什麼使用'?callback =?'?你在使用JSONP嗎? – Maxx
'getJSON'將發出'GET'請求而不是POST。要發佈POST請求,您可能想要使用$ .ajax或$ .post和JSONP參數化 – devnull69