1

I have problem with getitng correct response from mailchimp API V2.0.

When I try subscribe new user

var arr = {
           apikey:"xxxx",
           id:"secretListId",
           email:{
                  email:"jd@example.com"
                 },
           double_optin:"false"
          }

function jsonpCallback (){
       alert("jsonpCallback");
    };

$.ajax({
      type: 'GET',
      url: 'https://us6.api.mailchimp.com/2.0/lists/subscribe.json',
      dataType: 'jsonp',
      jsonpCallback: 'jsonpCallback',
      data: arr,
      timeout: 4000,
      cache: false,
      async: false, success: function(data, textStatus, jqXHR) {
        alert('success: '+data);
      },
      error: function(jqXHR, textStatus, errorThrown){
        alert('error: '+JSON.stringify(jqXHR));
      },
      complete: function(jqXHR, textStatus){
        alert('complete: '+JSON.stringify(jqXHR));
      }       
    }).success(function(rsp){
        console.log('success: '+rsp);
    }).fail(function(xhr,error,thrownError){
        console.log('fail  status:[' + xhr.status + ']  error:[' + thrownError + ']');
    }).always(function(){
        console.log('complete');
    });
});

In inspector I'm getting error 500.

And in alert popup

In this popup shold be another message (with error 500).

But when I past this link to browser I get:

{
"status": "error",
"code": 214,
"name": "List_AlreadySubscribed",
"error": "jd@example.com is already subscribed to list mpowroznik.com List. Click here to update your profile."

}

Of course if I add new e-mail I get popup alert

Why this is ERROR, not success ?

I'm using only jQuery without PHP or other language.

Function jsonpCallback isn't execute at all.

What I must do, to get correct response message.

Community
  • 1
  • 1
mpowroznik
  • 1,012
  • 7
  • 9
  • The error you are getting suggests you are using the api incorrectly. The api is returning a 500 error according to the inspector. – Kevin B Jan 31 '14 at 22:57
  • http://blog.mailchimp.com/mailchimps-api-v2/#restful They always return 500 if there is error. But in popup i can see only 404. – mpowroznik Jan 31 '14 at 23:00
  • if it's returning a 500 then that's why it's going to the error callback... – Kevin B Jan 31 '14 at 23:06
  • I see. you are incorrectly using jsonp. you shouldn't be defining a function named jsonpCallback, jquery will do that for you. If you wish to define your own callback, you'll want to use getScript instead. – Kevin B Jan 31 '14 at 23:08
  • 1
    I'm also beginning to get the impression that this api doesn't support JSONP, which goes along with the idea of *NEVER USING AN API KEY ON THE CLIENT* because the client can see/steal it, allowing anyone who uses the page that uses this request to steal your mailing list. – Kevin B Jan 31 '14 at 23:11
  • Try this answer > http://stackoverflow.com/questions/21526408/mailchimp-subscribe-using-jquery-ajax – Nagra Feb 06 '14 at 09:17

2 Answers2

4

This API is not meant to be used by code in the browser for a very specific reason.

By making this request in the browser, anyone who uses the page that makes this request will be able to take your api key and make their own requests to the api as if they were you, thus allowing anyone to steal all of the email addresses that are added to your list (and even empty the list.)

You must interact with this api using a server-side language such as PHP. Doing otherwise is compromising the security of your users/customers.

Kevin B
  • 94,570
  • 16
  • 163
  • 180
  • You have absolutely right! Now I using it to learning! I wanna know why it doesn't work as I want. Which piece of code is wrong, or maybe everything is wrong and I should try write this in other way. When I get response from API during adding new user i also getting alert with error :( – mpowroznik Jan 31 '14 at 23:22
  • That's because the service isn't returning jsonp, and you're requesting that it does. It will never return jsonp. Since it doesn't support JSONP or CORS, it isn't possible to do this with client-side code. – Kevin B Jan 31 '14 at 23:23
1
function jsonpCallback(data){
        alert(JSON.stringify(data[1]));
        return data;
    }

$.ajax({
    type: 'POST',
    url: 'http://xxx.us6.list-manage.com/subscribe/post-json?u=copiedFromActionForm&id=idList&c=?',
    data: data,
    cache: false,
    dataType: 'jsonp',
    success: function(data, text, xhr){
        alert(JSON.stringify(data.msg));

    },
    error: function (xhr, text, error) {
        console.log('error')
        console.log(JSON.stringify(xhr.msg));
    }
  });

In the alert popup you will get message.

Idea of solution taken from https://github.com/scdoshi/jquery-ajaxchimp

mpowroznik
  • 1,012
  • 7
  • 9
  • JSONP + POST doesn't make sense. otherwise this is likely the correct way to do it, if you corrected that ambiguity. – Kevin B Feb 23 '16 at 18:48