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: '.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));
},
plete: function(jqXHR, textStatus){
alert('plete: '+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('plete');
});
});
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": "[email protected] is already subscribed to list mpowroznik 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.
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./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));
},
plete: function(jqXHR, textStatus){
alert('plete: '+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('plete');
});
});
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": "[email protected] is already subscribed to list mpowroznik. 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.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 31, 2014 at 22:49 mpowroznikmpowroznik 1,0227 silver badges9 bronze badges 6- 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 Commented Jan 31, 2014 at 22:57
- blog.mailchimp./mailchimps-api-v2/#restful They always return 500 if there is error. But in popup i can see only 404. – mpowroznik Commented Jan 31, 2014 at 23:00
- if it's returning a 500 then that's why it's going to the error callback... – Kevin B Commented Jan 31, 2014 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 Commented Jan 31, 2014 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 Commented Jan 31, 2014 at 23:11
2 Answers
Reset to default 4This 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 promising the security of your users/customers.
function jsonpCallback(data){
alert(JSON.stringify(data[1]));
return data;
}
$.ajax({
type: 'POST',
url: 'http://xxx.us6.list-manage./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./scdoshi/jquery-ajaxchimp