【问题标题】:MailChimp API 3.0 xhr subscribe 501 errorMailChimp API 3.0 xhr 订阅 501 错误
【发布时间】:2017-01-27 21:13:41
【问题描述】:

我正在尝试使用这个 sn-p 为用户订阅我的 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 的服务来更新列表。因为你们没有添加访问控制标头。我无法使用现代浏览器向您的端点发送简单的 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 解决方案。

【问题讨论】:

    标签: javascript ajax mailchimp mailchimp-api-v3.0


    【解决方案1】:

    他们目前不允许客户端访问他们的 API。他们的response to a similar question in comments

    我们不支持通过客户端 Javascript 访问 API 以 避免将 API 密钥传递给用户的安全问题。

    【讨论】:

      猜你喜欢
      • 2015-08-13
      • 2017-05-05
      • 2015-11-24
      • 2019-05-16
      • 2018-02-11
      • 2013-11-09
      • 2015-06-02
      • 2018-06-22
      • 2017-01-04
      相关资源
      最近更新 更多