I'm using FormKeep http://ift.tt/1uuJqVb, and when I submit the form it redirects to a thank you page. But I want it to stay on the page and submit via AJAX. Below is my code, but in order to disable the redirect, they say I have to set the Accept header of the request to application/javascript.
How is this supposed to be done?
Can I submit forms with AJAX?
Yes, FormKeep accepts Cross Origin Requests. To disable the redirect, set the Accept header of the request to application/javascript.
//Simple form
$("form").submit(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'http://ift.tt/1bH4GVQ',
data: $(this).serialize(),
success: function() {
//window.location = "http://google.com";
alert('Success!');
},
error: function() {
alert('Error!');
}
});
});
// ---------------------------------
// A more advanced form, with success message
var form = $("form.ajax");
form.submit(function(e) {
e.preventDefault();
if ($("body").hasClass("support")) {
var formkeepID = "MyID"; //support
var successMsg = " You're in the front of the line! You should be hearing from us soon. ";
} else if ($("body").hasClass("partner-with-us")) {
var formkeepID = "MyID"; //partner-with-us
var successMsg = "Thank you! We'll get back to you shortly on partnering with us";
}
$.ajax({
type: 'POST',
url: 'https://formkeep.com/f/' + formkeepID,
data: $(this).serialize(),
success: function() {
form.find("input, button, textarea").prop('disabled', true).css({"opacity": 0.5});
form.find(".alert-success").text(successMsg).fadeIn(500);
},
error: function() {
alert('Error!');
}
});
});
Aucun commentaire:
Enregistrer un commentaire