Mailchimp - 添加新订阅者(JQuery + AJAX)

时间:2016-02-22 20:27:06

标签: javascript ajax mailchimp

我一直在尝试通过下面的JS函数连接到MailChimp 3.0 API:

    var mailchimp_api_key = (api key as in my account);
    var name = $('#name').val();
    var email = $('#email').val();

    if(name.indexOf(' ') != 0) {
        var fname = name.substr(0, name.indexOf(' '));
        var lname = name.substr(name.indexOf(' ') + 1);
    } else {
        fname = name;
    }

    var mailchimp = JSON.stringify({
        email_address: email,
        status: 'subscribed',
        merge_fields: {
            FNAME: fname,
            LNAME: lname
        }
    });

    $.ajax({
        url: 'http://(my server in MC).api.mailchimp.com/3.0/lists/(list id)/members/',
        type: 'POST',
        data: mailchimp,
        dataType: 'jsonp',
        contentType: 'application/json; charset=utf-8',
        error: function(res, text){
            console.log('Err', res);
        },
        success: function(res){
            console.log('Success', res);
        },
        beforeSend: function (xhr) {
            xhr.setRequestHeader ("Authorization", "Basic " + btoa("api:" + mailchimp_api_key))
        }
    });

我的问题是我一直收到401错误,而我没有做任何纠正。

我是本地的,不使用服务器。

0 个答案:

没有答案