1

I'm trying to connect an html page with my Mailchimp (free) account. I've read the documentation and should be quite simple.

This is my JS code (jQuery 1.2.2 loaded on page load):

username = [your-username];
apiKey = [your-mailchimp-api-key];
baseUrlMailchimp = "https://us5.api.mailchimp.com/3.0/";
apiMC = function(type, data, url, callback) {
            $.ajax({
                type: type,
                dataType: 'jsonp',
                data: data,
                contentType: 'application/json',
                url: url,
                beforeSend: function(xhr) {
                    xhr.setRequestHeader('Authorization', 'Basic ' + btoa(username + ":" + apiKey));
                },
                success: function(e) {
                    if (callback) {
                        callback(e)
                    }
                },
                error: function(e) {
                    console.log(e);
                }
            });
        }
apiMC('GET', '', baseUrlMailchimp + 'lists', function(res){console.log(res)})

The problem is that I receive an error 401 (Your request did not include an API key).

If I launch the same request from my Chrome REST client:

http://restclient.net/

specifying the url and user/api in the basic auth fields, everything works fine!

I cannot understand why, can anyone help me?

UPDATE: ANSWER FROM MAILCHIMP

Hey there Francesco,

I took a look at the article link provided and it does look like you are running into a same Origin policy block. The same origin policy prevents ajax from calling its non-origin domains. Because of this you would need to use a server side method to fire off API calls.

Also, at this time MailChimp does not support Jsonp for API 3.0.

If there is anything else at all with which I can assist, please do not hesitate to reach back out to support.

END

1 Answer 1

1

I think you might be running into Same Origin Policy. I don't think you can call another domain from AJAX. You will likely need to implement your call server side, maybe through a gateway file.

https://en.wikipedia.org/wiki/Same-origin_policy

6
  • But this API's are public... how can override the same origin policy in these cases?
    – frankcecca
    Commented Apr 22, 2016 at 12:10
  • 1
    Has nothing to do with MC having a public API. It's not MailChimp that is blocking you in this case. Same Origin Policy is enforced by the client. You will want to read the linked article there. The way around this is to make these requests server side and not through AJAX. This is why it is working in your REST client and not on your site.
    – Hutch
    Commented Apr 22, 2016 at 12:46
  • Ok, I'm reading the doc. Probably you're right, but it seems strange that with jQuery ajax method this policy cannot be bypassed. I've searched for long time and I've seen that previously were released (for version 2.0 api) many libraries that integrated this kind of implementation. Consider that ajax method provide a JSONP data type that (in theory) should let solve the cross domain restriction
    – frankcecca
    Commented Apr 22, 2016 at 12:50
  • 1
    Yeah It think MC is still working on various libraries for 3.0. I don't know if any of those are intended to address client side languages. However if it makes you life a little bit easier I have been working on a comprehensive PHP library for version 3.0 here: [github.com/Jhut89/MailChimp-API-3.0-PHP-Wrapper] (github.com/Jhut89/MailChimp-API-3.0-PHP-Wrapper)
    – Hutch
    Commented Apr 22, 2016 at 12:59
  • I've added the answer from Mailchimp in my post. You were right, thanks
    – frankcecca
    Commented Apr 22, 2016 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.