I'm trying to submit a form to Campaign Monitor. They offer this code example to POST via Ajax.
This is my code for my multi-step modal.
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'); } }) }); } }); On the last step of the form, I check whether final_step is true, if so, go ahead and submit the form via ajax.
The problem is that it just doesn't do anything? But if I remove the e.preventDefault(); from the $('#myform') it will post the form as normal and re-direct you to the form URL.
How can I fix this?
if (data.Status === 400) {condition and check response existance?callback=?? Are you using JSONP?getJSONwill issue aGETrequest rather than a POST. To issue a POST request you might want to use $.ajax or $.post with JSONP parametrization?callback=?because it's the code that was provided by Campaign Monitor. You can see the gist by clicking the link at the top of the page.