Mailchimp API V3 jQuery Ajax POST订阅者 [英] Mailchimp API V3 jQuery Ajax POST subscribers
问题描述
Mailchimp API(v3.0)进行了重大更新,许多jQuery插件已经过时,以便form.submit()
上的POST
个订阅者.
There has been a big update on the Mailchimp API (v3.0) and many of the jQuery plugins are out of date in order to POST
subscribers on form.submit()
.
阅读v3.0文档后:
管理订户建议使用以下JSON对象格式:
Managing subscribers suggests the following JSON object format:
{
"email_address": "urist.mcvankab@freddiesjokes.com",
"status": "subscribed",
"merge_fields": {
"FNAME": "Urist",
"LNAME": "McVankab"
}
}
该API的以下根端点列出了资源:
And the following root endpoint for the API lists resource:
https://<dc>.api.mailchimp.com/3.0/
这是我的form.submit()
代码和jQuery Ajax POST
请求:
So here's my form.submit()
code with the jQuery Ajax POST
request:
$(document).ready(function(){
var mcForm = $('#mailchimpForm');
var mailchimp = {};
mailchimp.dc='us5';
mailchimp.id='xxxxxxxx';
var url = '//' + mailchimp.dc + '.api.mailchimp.com/3.0/lists/' + mailchimp.id + '/members/';
function beginMailchimpPost(data){
var params = JSON.stringify(data);
$.ajax({
url: url,
method: 'POST',
data: params,
dataType: 'jsonp',
contentType: 'application/json; charset=utf-8',
error: function(res, text){
console.log('Err', res);
},
success: function(res){
console.log('Success', res);
}
});
}
});
这是JSON.stringify(data)
对象:
{"email_address":"email@mail.com","status":"subscribed","merge_fields":{"FNAME":"Name","LNAME":"Last name"}}
我收到以下错误:
GET http://... 401 (Unauthorized)
Err Object {readyState: 4, status: 404, statusText: "error"}
有什么问题吗?
这是 Mailchimp的API v3.0文档(列表成员)的链接集合).
Here's the link to Mailchimp's API v3.0 docs (list members collection).
推荐答案
我这样做的方法是使用您的AJAX代码,但删除所有MailChimp内容,然后将发布数据发送到PHP文件.我使用了以下代码:
The way I did it is to use your AJAX code but strip out all the MailChimp stuff and send the post data to a PHP file. I used this code:
我只是去除了所有内容,除了订阅单个用户所需的部分外,它的工作就像是一种魅力.对于错误报告,您可能可以在PHP端检测到错误并将HTTP状态发送到AJAX.
I just stripped out everything but the part I needed for subscribing a single user and it worked like a charm. For error reporting, you can probably detect for errors on the PHP side and send HTTP status to the AJAX.
这篇关于Mailchimp API V3 jQuery Ajax POST订阅者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!