Mailchimp API getting error and can't use ajax success

白昼怎懂夜的黑 提交于 2019-12-11 12:39:13

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/21491515/mailchimp-api-getting-error-and-cant-use-ajax-success

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!