Skip to content

Instantly share code, notes, and snippets.

@crismanNoble
Last active September 29, 2015 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crismanNoble/42fc1c6c7d0b20bba8bb to your computer and use it in GitHub Desktop.
Save crismanNoble/42fc1c6c7d0b20bba8bb to your computer and use it in GitHub Desktop.
Simple form AJAX example
$('document').ready(function(){
$('.mc-form-example').submit(function(e){
//prevent the form from submitting via the browser redirect
e.preventDefault();
//grab attributes and values out of the form
var data = {email: $('#mc-email').val()};
var endpoint = $(this).attr('action');
//make the ajax request
$.ajax({
method: 'POST',
dataType: "json",
url: endpoint,
data: data
}).success(function(data){
if(data.id){
//successful adds will have an id attribute on the object
alert('thanks for signing up');
} else if (data.title == 'Member Exists') {
//MC wil send back an error object with "Member Exists" as the title
alert('thanks, but you are alredy signed up');
} else {
//something went wrong with the API call
alert('oh no, there has been a problem');
}
}).error(function(){
//the AJAX function returned a non-200, probably a server problem
alert('oh no, there has been a problem');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment