Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mailchimp API V3 jQuery Ajax POST subscribers

There has been a big update on the Mailchimp API (v3.0) and many of the jQuery plugins are out of date in order to POST subscribers on form.submit().

After reading v3.0 docs:

Managing subscribers suggests the following JSON object format:

{
   "email_address": "[email protected]", 
   "status": "subscribed", 
   "merge_fields": {
      "FNAME": "Urist", 
      "LNAME": "McVankab"
   }
}

And the following root endpoint for the API lists resource:

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

So here's my form.submit() code with the jQuery Ajax POST request:

$(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 protected]","status":"subscribed","merge_fields":{"FNAME":"Name","LNAME":"Last name"}}

And I'm getting the following error:

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

What could be wrong?

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

like image 718
Gus Avatar asked Sep 25 '15 08:09

Gus


1 Answers

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.

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

like image 67
Daniel Nass Avatar answered Oct 11 '22 16:10

Daniel Nass