Mailchimp API V3 jQuery Ajax POST subscribers

A major update has appeared in the Mailchimp API (v3.0), and many of the jQuery plugins are deprecated to allow POST subscribers to form.submit() .

After reading v3.0 docs:

Subscriber management offers the following JSON object format:

 { "email_address": " urist.mcvankab@freddiesjokes.com ", "status": "subscribed", "merge_fields": { "FNAME": "Urist", "LNAME": "McVankab" } } 

And the following root endpoint for the API lists the resource:

https://<dc>.api.mailchimp.com/3.0/

So here is my form.submit() code with a jQuery Ajax POST :

 $(document).ready(function(){ var mcForm = $('#mailchimpForm'); var mailchimp = {}; mailchimp.dc='us5'; mailchimp.id='xxxxxxxx'; var url = '//' + mailchimp.dc + '.api.mailchimp.com/3.0/lists/' + mailchimp.id + '/members/'; function beginMailchimpPost(data){ var params = JSON.stringify(data); $.ajax({ url: url, method: 'POST', data: params, dataType: 'jsonp', contentType: 'application/json; charset=utf-8', error: function(res, text){ console.log('Err', res); }, success: function(res){ console.log('Success', res); } }); } }); 

This is the JSON.stringify(data) object:

 {"email_address":" email@mail.com ","status":"subscribed","merge_fields":{"FNAME":"Name","LNAME":"Last name"}} 

And I get the following error:

 GET http://... 401 (Unauthorized) Err Object {readyState: 4, status: 404, statusText: "error"} 

What could be wrong?

Here's a link to the Mailchimp API v3.0 docs (collection of list members).

+7
jquery ajax mailchimp
source share
4 answers

Unfortunately, it is not possible to execute queries for the Mailchimp front-end API.

Note. MailChimp does not support the implementation of our API on the client side using CORS requests due to a potential security risk when exposing API account keys.

https://developer.mailchimp.com/documentation/mailchimp/guides/get-started-with-mailchimp-api-3/#authentication

+2
source share

The way I did this was to use AJAX code, but cross out all MailChimp materials and send post data to a PHP file. I used this code:

https://github.com/actuallymentor/MailChimp-API-v3.0-PHP-cURL-example/blob/master/mc-API-connector.php

I just deleted everything except the part that I need to subscribe to a single user, and it worked like a charm. For error reporting, you can detect errors on the PHP side and send the HTTP status to AJAX.

+2
source share

You get 401 because you are not passing your API key.

You need to add the following: ajax call:

 beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " + btoa("api:" + mailchimp_api_key)); }; 

Where mailchimp_api_key is the key for your account. See http://kb.mailchimp.com/api/article/api-3-overview for more information about auth using the api.

+1
source share

You need to add your API key to the parameters, for example:

 { "apikey": "your key here", "email_address": " urist.mcvankab@freddiesjokes.com ", "status": "subscribed", "merge_fields": { "FNAME": "Urist", "LNAME": "McVankab" } } 

Then you need to change the datetype type from "jsonp" to "json". "jsonp" is only GET and will not work with the POST method.

And the last thing you need to do is enable cross-domain access scripts;

http://enable-cors.org/server.html

-one
source share

All Articles