我正在尝试使用此代码段订阅用户到我的mailchimp列表:
function subscribeUser(name, email) {
var xhr = new XMLHttpRequest();
var endpoint = 'https://<dc>.api.mailchimp.com/3.0/lists/<list>/members/';
var data = {};
data.email_address = email;
data.status = "subscribed";
data.merge_fields = {};
data.merge_fields.NAME = name;
xhr.open("POST", endpoint, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Authorization", "apikey <key>");
xhr.onreadystatechange = function() {
if(xhr.readyState == 4 && xhr.status == 200) {
alert(xhr.responseText);
}
}
xhr.send(data);
}
它在chrome中生成此错误:
我无法编写基于ajax的服务来更新列表。因为你们没有添加Access Control标头。我无法使用现代浏览器向您的终端发送简单的xhr。
XMLHttpRequest cannot load https://<dc>.api.mailchimp.com/3.0/lists/<list>/members/. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8000' is therefore not allowed access. The response had HTTP status code 501.
我是一个静态的网站,我想保持这种方式。在github上托管不需要后端。所以我需要一个JS解决方案。
答案 0 :(得分:0)
他们目前不允许客户端访问其API。他们的response to a similar question in comments:
我们不支持通过客户端Javascript访问API 避免将API密钥传递给用户的安全问题。