Mailchimp API(v3.0)有一个很大的更新,许多jQuery插件已经过时,以便在form.submit()上发布POST订阅者.
阅读v3.0文档后:
Managing subscribers建议使用以下JSON对象格式:
{ "email_address": "urist.mcvankab@freddiesjokes.com","status": "subscribed","merge_fields": { "FNAME": "Urist","LNAME": "McVankab" } }
以下API的根端点列出了资源:
HTTPS://< DC> .api.mailchimp.com / 3.0 /
所以这是我的form.submit()代码与jQuery Ajax POST请求:
$(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(数据)对象:
{"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发出请求.
Note MailChimp does not support client-side implementation of our API
using CORS requests due to the potential security risk of exposing
account API keys.