2016-09-20 93 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解決方案。

回答