4

I'm trying to make a GET request through jQuery to the Mailchimp API. It seems though my custom header is not correctly set as I get a Your request did not include an API key. error.

It works fine if I make the request using curl on my Ubuntu machine:

curl --header "Authorization: apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11" https://us11.api.mailchimp.com/3.0/campaigns

Here's my code:

$.ajax({
    type: 'GET',
    url: 'https://us11.api.mailchimp.com/3.0/campaigns',
    crossDomain: true,
    dataType: 'jsonp',
    contentType: "application/json; charset=utf-8",
    headers: {
        'Authorization': 'apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11'
    }
}).done(function (response) {
    console.log(response); // verbose
});

I even tried adding this above:

$.ajaxSetup({
    headers: { 'Authorization': 'apikey 709XXXXXXXXXXXXXXXXXXXXXXXXXXXXX-us11' }
});
Tim Malone
  • 3,188
  • 4
  • 34
  • 47
MultiformeIngegno
  • 6,841
  • 14
  • 57
  • 111

2 Answers2

3

You need to add the key via Basic Auth like and as far I am aware off, You can't query it from front-end, it must be on the back-end.

Find an example in NodeJS:

headers: {
    'Authorization': 'Basic ' + new Buffer(`anything:${MailChimpKey}`).toString('base64');
}
Hardik Shah
  • 3,726
  • 2
  • 18
  • 40
rubelux
  • 61
  • 1
  • 6
-1

MailChimp not allowed to direct access with ajax. Once make Server WebRequest. It will surely work.

Jainish Jariwala
  • 308
  • 2
  • 14