Reputation: 1022
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:"[email protected]"
},
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.
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": "[email protected] 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.
Upvotes: 1
Views: 6028
Reputation: 1022
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
Upvotes: 1
Reputation: 95017
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.
Upvotes: 4