gpt4 book ai didi

javascript - MailChimp AJAX API 未调用 API

转载 作者:行者123 更新时间:2023-11-28 05:37:10 25 4
gpt4 key购买 nike

所以我尝试集成 MailChimp 的 API,这样订阅时就不会重定向到 MailChimp 网站。我正在使用这个tutorial 。每当我尝试订阅时,网站都会回显一条错误消息“哦不,出现了问题”。

在该网站的代码中,这要么意味着 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:

<?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;
?>

HTML:

<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>

最佳答案

如果您传递的 apikey 类似于 bxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf-us10,则请求将不会成功。

尝试通过跳过连字符后面的字符(例如 bxxxxxxxxxxxxxxxxxxxxxxxxxxxxx5dxf)来传递 apikey,并检查它是否有效。

在本例中编辑您的 php 代码

$api_key = 'realapikey-us14';

$api_key = 'realapikey';

关于javascript - MailChimp AJAX API 未调用 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39267770/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com