7

I am trying to subscribe an email to a list on mailchimp, I followed the documentation first, made a request using "Postman" added what was needed and everything works just fine, so I tried to do it on my website and it didn't work

I tried to made a simple request with the same values I set on postman, but everytime I try to send the request the response says

XMLHttpRequest cannot load https://us12.api.mailchimp.com/3.0/lists/xxxxxx/members. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://mywebsite.com' is therefore not allowed access. The response had HTTP status code 501.

I tried to find a way to overcome this but it has been impossible

I searched on stackoverflow everybody says to use jsonp or add something to the ajax call or use a mailchimp ajax plugin nothing has worked

I tried diferent stackoverflow posts like this one Mailchimp subscribe using jQuery AJAX? but almost all of them say the same

I tried cache: false dataType:jsonp crossDomain: true xhrFields: {withCredentials: true}

Here it is my code, I am using Jquery

$.ajax({
          type: "POST",
          url: "https://usxx.api.mailchimp.com/3.0/lists/xxxxxxxx/members",

          data: { "email_address":email@adress.com,  "status":"subscribed"},
          headers: {
            "Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx==",
            "Content-Type": "application/json"
          },
          success: function(data){
             alert('Thanks for subscribing');
          },
          error: function(data){
            alert('there was an error, try again later');
          }
});

I also Thought on creating my own api and then make the call to mailchimp api but I might ran into the same problem

Do you have any suggestions?

Thanks in advance

Community
  • 1
  • 1
Hyra10
  • 460
  • 2
  • 6
  • 18
  • 1
    Please do a search for that error. It is CORS problem and this issue gets asked numerous times a day. If API isn't CORS or jsonp enabled you need to use a proxy on your server – charlietfl May 27 '16 at 23:56
  • @charlietfl I had searched everywhere and not all of them suggested to use a proxy, thanks for your comment – Hyra10 May 30 '16 at 17:58

1 Answers1

11

As charliefl noted, this is a CORS issue. MailChimp doesn't support CORS, mostly because it would require you passing your API credentials to the user of the webpage, allowing them to takeover your entire account.

Your two options for MailChimp are to proxy your requests through a server or, for signing people up to your list, you can build a custom signup form that uses a much more restricted API. The caveat of this second method is that it forces all of your subscribes through MailChimp's double opt-in process.

Community
  • 1
  • 1
TooMuchPete
  • 4,583
  • 2
  • 17
  • 21
  • I had seen that post, you wrote, but jsonp doesn't work on api 3.0 so my only option here is a proxy thanks for your help! – Hyra10 May 30 '16 at 19:31
  • This is a silly excuse. There are other email subscription services and approaches that allow CORS interactions without exposing private keys. They just have to be built and tested. – Dtipson Mar 23 '20 at 18:28
  • 3
    It's not an excuse, it's a description of the system in question. It is, of course, possible to build a different system with different capabilities, but this is StackOverflow, not a MailChimp feature request website, so the answer here is directed toward helping the asker resolve their issue. – TooMuchPete Mar 24 '20 at 19:20