Mailchimp API V3 jQuery Ajax POST subscribers
Unfortunately it is not possible to make requests for the front-end Mailchimp API.
Note MailChimp does not support client-side implementation of our API using CORS requests due to the potential security risk of exposing account API keys.
The way I did it is to use your AJAX code but strip out all the MailChimp stuff and send the post data to a PHP file. I used this code:
I just stripped out everything but the part I needed for subscribing a single user and it worked like a charm. For error reporting, you can probably detect for errors on the PHP side and send HTTP status to the AJAX.
You're getting a 401 because you're not passing in your API key.
You'll need to add the following to you're ajax call:
beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("api:" + mailchimp_api_key)); };
Where mailchimp_api_key is the key for your account. Take a look at http://kb.mailchimp.com/api/article/api-3-overview for more information on auth with the api.