I’ve found some good articles to guide me on how to post JSON data using javascript, this one was most to the point: How can I use JQuery to post JSON data?. Unfortunately I’m still not able to get a successful post. Here’s what I have right now, though I eventually will need to replace “email@gmail.com” with the variable from the email input field.
<!-- ========== START Mail Signup PopUp ========== -->
<div id="popup-1" class="slickModal">
<div class="window">
<div class="wrap demo-1">
<div class="title">SAVE 15% TODAY</div>
<li>Exclusive Flash Sales</li>
<li>Giveaways</li>
<li>Free Shipping Coupons</li>
<li>and more!</li>
<br><br>
<input type="email" value="" id="popupEmail" class="required email field" placeholder="Your email goes here">
<input type="submit" value="submit" onclick="mcTest()">
<script>
function mcTest() {
$.ajax({
type: "POST",
url: "https://us13.api.mailchimp.com/3.0/lists/123456/members/",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: JSON.stringify({"email_address": "email@gmail.com","status": "subscribed"}),
success: function (data)
{
alert("success!") + data;
},
error: function()
{
alert("Cannot get data");
}
});
}
</script>
</div>
<div class="cta demo-1">
<span class="icon"></span>
</div>
</div>
</div>
<!-- ========== END Mail Signup PopUp ========== -->
I keep getting the error “Cannot get data” error popup. I’m bundling and serving up the json2.js file in order to get the stringify function to work, and I’ve tested the call as a cURL cmd that also works:
curl --request POST --url "https://us13.api.mailchimp.com/3.0/lists/123456/members/" --user "chris:123456789123456789-us13" --header "content-type: application/json" --data @C:\curl\mc.txt
where this is the content of the mc.txt file:
{
"email_address": "email@gmail.com",
"status": "subscribed"
}
Any thoughts on what I’m doing wrong? Thank you for any help/suggestions!