Skip to content

Instantly share code, notes, and snippets.

@icantrank
Last active September 18, 2017 20:21
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 icantrank/413cd212ec85e48a8b16a36f76bbc539 to your computer and use it in GitHub Desktop.
Save icantrank/413cd212ec85e48a8b16a36f76bbc539 to your computer and use it in GitHub Desktop.
Mailchimp subscription with ajax and jsonp (and publicly visible apikey but who gives af)
<!doctype html>
<!-- This works, but it's sketchy and leaves ur api key available for stealing -->
<!-- 17 revisions coz 8 tabs wtf github? -->
<head>
<title>Scetchy mailchimp form</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
</script>
</head>
<body>
<form id="mailchimp">
<input type="email" name="EMAIL" />
<input type="submit" />
</form>
<script>
var listId = '123456',
username = 'username',
server = 'us1',
apiKey = '12345678abcdefg'
var url = '//' + username + '.' + server + '.' + 'list-manage.com/subscribe/post-json?u=' + apiKey + '&amp;id=' + listId + '&c=?'
$('form#mailchimp').submit(function(e) {
e.preventDefault();
$.ajax({
url: url,
data: $('form#mailchimp').serialize(),
dataType: 'jsonp',
success: function(data) {
console.log(JSON.stringify(data))
if (data['result'] == 'success') {
$('form#mailchimp').append('<p>' + data['msg'] + '</p>');
} else {
$('form#mailchimp').append('<p>Error something went wrong</p>')
}
}
})
})
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment