0

I need to pass subscribers to mailchimp. My best idea so far, as the project already uses jQuery, is to use $.post(), but I am not able to convert the function inside the docs into my working version. I got three questions:

  1. How can I authenticate myself using my API key?
  2. How can I send the userData?
  3. How can I display a success or failure message?

Mailchimp recommends me to:

To add someone to your list, send a POST request to the List Members endpoint: /3.0/lists/9e67587f52/members/. The request body should be a JSON object that has the member information you want to add, with status and any other required list fields.

I have stored all necessary data inside my code:

var API_BASE = 'https://us6.api.mailchimp.com/3.0';
var LIST_ID = '********';

//combine to post link
var POST_URL = API_BASE + '/lists/' + LIST_ID + '/members/'

// API_KEY for authentication
var API_KEY = '*******************-us6'

// userData to send to mailchimp
var userData = {
  "email_address": self.mailAddress,
  "status": "subscribed",
  "merge_fields": {
    "BANKNAME": self.bankName,
    "PHONESYS": self.phoneSystem
  }
}

The data already follows mailchimps guidelines for posting JSON objects. How can I use these data combined with jQuery.post( url \[, data \] \[, success \] \[, dataType \] )?

Although I have seen in the docs, that there are .done() and .fail messages. Can I use these to give the user a response like "success" or "error"?

Marian Rick
  • 3,350
  • 4
  • 31
  • 67
  • How are you supposed to pass the API key? – Musa Jun 22 '17 at 19:28
  • The answer is **you can't do this**. Read this: https://stackoverflow.com/a/40494574/1819684 That big warning in the middle of that API that you supposedly read says it all. – gforce301 Jun 22 '17 at 19:31
  • You should instead use jquery to send an ajax post request to your server and handle it there. I'd assume your server is utilizing php, so you could send all of your data via php. – Dan Zuzevich Jun 22 '17 at 19:33
  • @DanielZuzevich thanks thats a really good suggestion! – Marian Rick Jun 22 '17 at 19:34
  • @MarianRick I've used this library before to make it easy to do a lot with MailChimp, its pretty good. https://github.com/drewm/mailchimp-api – Dan Zuzevich Jun 22 '17 at 20:15

0 Answers0