2016-09-20 69 views
0

我试图用这个片段到订阅用户到我的mailchimp名单:MailChimp API 3.0 XHR认购501错误

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解决方案。

回答