Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript MailChimp AJAX API未调用API_Javascript_Php_Jquery_Ajax_Mailchimp - Fatal编程技术网

Javascript MailChimp AJAX API未调用API

Javascript MailChimp AJAX API未调用API,javascript,php,jquery,ajax,mailchimp,Javascript,Php,Jquery,Ajax,Mailchimp,因此,我正在尝试集成MailChimp的API,以便在订阅时不会重定向到MailChimp网站。我在用这个。每当我尝试订阅时,网站都会回应一个错误:“哦,不,有问题了” 在该站点上的代码中,这要么意味着API调用出现了问题,要么AJAX函数返回了非200。问题是我在代码中找不到问题。我可能在代码中插入了错误的信息,但我找不到任何东西 JavaScript: $('document').ready(function () { $('form').submit(function (e) {

因此,我正在尝试集成MailChimp的API,以便在订阅时不会重定向到MailChimp网站。我在用这个。每当我尝试订阅时,网站都会回应一个错误:“哦,不,有问题了”

在该站点上的代码中,这要么意味着API调用出现了问题,要么AJAX函数返回了非200。问题是我在代码中找不到问题。我可能在代码中插入了错误的信息,但我找不到任何东西

JavaScript:

$('document').ready(function () {
    $('form').submit(function (e) {
        //prevent the form from submitting via the browser redirect
        e.preventDefault();
        //grab attributes and values out of the form
        var data = {
            email: $('#mc-email').val()
        };
        var endpoint = $(this).attr('action');
        //make the ajax request
        $.ajax({
            method: 'POST'
            , dataType: "json"
            , url: endpoint
            , data: data
        }).success(function (data) {
            if (data.id) {
                //successful adds will have an id attribute on the object
                alert('thanks for signing up');
            }
            else if (data.title == 'Member Exists') {
                //MC wil send back an error object with "Member Exists" as the title
                alert('thanks, but you are alredy signed up');
            }
            else {
                //something went wrong with the API call
                alert('oh no, there has been a problem');
            }
        }).error(function () {
            //the AJAX function returned a non-200, probably a server problem
            alert('oh no, there has been a problem');
        });
    });
});
<?php
//fill in these values for with your own information
$api_key = 'realapikey-us14';
$datacenter = 'us14';
$list_id = 'reallistid';
$email = $_POST['email'];
$status = 'subscribed';
if(!empty($_POST['status'])){
    $status = $_POST['status'];
}
$url = 'https://'.$datacenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/';
$username = 'apikey';
$password = $api_key;
$data = array("email_address" => $email,"status" => $status);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$api_key");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>
<form action="./endpoint.php" method="POST" id="form mailchimp">
            <input type="hidden" name="u" value="VALUEFORLIST">
            <input type="hidden" name="id" value="LISTID">
            <input name="FNAME" class="popupname" type="text" value="" placeholder="Naam:" required/>
            <br>
            <input id="mc-email" name="EMAIL" class="popupemail" type="email" value="" placeholder="Email:" required/>
            <br>
            <input name="action" class="popupsubmit" type="submit" value="Aanmelden" class="submit" /> 
</form>
PHP:

$('document').ready(function () {
    $('form').submit(function (e) {
        //prevent the form from submitting via the browser redirect
        e.preventDefault();
        //grab attributes and values out of the form
        var data = {
            email: $('#mc-email').val()
        };
        var endpoint = $(this).attr('action');
        //make the ajax request
        $.ajax({
            method: 'POST'
            , dataType: "json"
            , url: endpoint
            , data: data
        }).success(function (data) {
            if (data.id) {
                //successful adds will have an id attribute on the object
                alert('thanks for signing up');
            }
            else if (data.title == 'Member Exists') {
                //MC wil send back an error object with "Member Exists" as the title
                alert('thanks, but you are alredy signed up');
            }
            else {
                //something went wrong with the API call
                alert('oh no, there has been a problem');
            }
        }).error(function () {
            //the AJAX function returned a non-200, probably a server problem
            alert('oh no, there has been a problem');
        });
    });
});
<?php
//fill in these values for with your own information
$api_key = 'realapikey-us14';
$datacenter = 'us14';
$list_id = 'reallistid';
$email = $_POST['email'];
$status = 'subscribed';
if(!empty($_POST['status'])){
    $status = $_POST['status'];
}
$url = 'https://'.$datacenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/';
$username = 'apikey';
$password = $api_key;
$data = array("email_address" => $email,"status" => $status);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$api_key");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>
<form action="./endpoint.php" method="POST" id="form mailchimp">
            <input type="hidden" name="u" value="VALUEFORLIST">
            <input type="hidden" name="id" value="LISTID">
            <input name="FNAME" class="popupname" type="text" value="" placeholder="Naam:" required/>
            <br>
            <input id="mc-email" name="EMAIL" class="popupemail" type="email" value="" placeholder="Email:" required/>
            <br>
            <input name="action" class="popupsubmit" type="submit" value="Aanmelden" class="submit" /> 
</form>

HTML:

$('document').ready(function () {
    $('form').submit(function (e) {
        //prevent the form from submitting via the browser redirect
        e.preventDefault();
        //grab attributes and values out of the form
        var data = {
            email: $('#mc-email').val()
        };
        var endpoint = $(this).attr('action');
        //make the ajax request
        $.ajax({
            method: 'POST'
            , dataType: "json"
            , url: endpoint
            , data: data
        }).success(function (data) {
            if (data.id) {
                //successful adds will have an id attribute on the object
                alert('thanks for signing up');
            }
            else if (data.title == 'Member Exists') {
                //MC wil send back an error object with "Member Exists" as the title
                alert('thanks, but you are alredy signed up');
            }
            else {
                //something went wrong with the API call
                alert('oh no, there has been a problem');
            }
        }).error(function () {
            //the AJAX function returned a non-200, probably a server problem
            alert('oh no, there has been a problem');
        });
    });
});
<?php
//fill in these values for with your own information
$api_key = 'realapikey-us14';
$datacenter = 'us14';
$list_id = 'reallistid';
$email = $_POST['email'];
$status = 'subscribed';
if(!empty($_POST['status'])){
    $status = $_POST['status'];
}
$url = 'https://'.$datacenter.'.api.mailchimp.com/3.0/lists/'.$list_id.'/members/';
$username = 'apikey';
$password = $api_key;
$data = array("email_address" => $email,"status" => $status);
$data_string = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$api_key");
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$result=curl_exec ($ch);
curl_close ($ch);
echo $result;
?>
<form action="./endpoint.php" method="POST" id="form mailchimp">
            <input type="hidden" name="u" value="VALUEFORLIST">
            <input type="hidden" name="id" value="LISTID">
            <input name="FNAME" class="popupname" type="text" value="" placeholder="Naam:" required/>
            <br>
            <input id="mc-email" name="EMAIL" class="popupemail" type="email" value="" placeholder="Email:" required/>
            <br>
            <input name="action" class="popupsubmit" type="submit" value="Aanmelden" class="submit" /> 
</form>




如果您像
BXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX5DXF-us10一样传递apikey,则请求将不会成功

通过跳过连字符后的字符(如
bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf
)尝试传递apikey,并检查其是否有效

在本例中,请从中编辑php代码

$api_key = 'realapikey-us14';


如果您像BXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX5DXF-us10一样传递apikey,则请求将不会成功

通过跳过连字符后的字符(如
bxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf
)尝试传递apikey,并检查其是否有效

在本例中,请从中编辑php代码

$api_key = 'realapikey-us14';


请提供HTML部分我已经添加了HTML部分@MähámmádYäsårk请提供HTML部分我已经添加了HTML部分@MähámmádYäsårk这,不幸的是,在我生成API代码时仍然会出现“哦,不,有问题”错误,默认情况下,它给了我us14。请您将其中一条错误消息更改为“哦,不,有一个问题2”,并检查显示的是哪个错误?我刚刚将第二条错误消息“非200”更改为问题2,它给了我该错误。我想这和服务器有关吧?我使用的方法仍然有效吗?我已经打开浏览器控制台,它给了我“jquery.js:9664 POST 404(Not Found)”,不幸的是,这仍然会给我“哦,不,有一个问题”错误。当我生成API代码时,它默认给了我us14。你能将其中一条错误消息更改为“哦,不,出现了一个问题2”并检查显示了哪个错误?我刚刚将第二个“non-200”更改为problem2,它给了我这个错误。我猜这与服务器有关?我使用的方法仍然有效吗?我打开了浏览器控制台,它给了我“jquery.js:9664 POST 404(未找到)”