【问题标题】:MailChimp Get Error Response using AjaxMailChimp 使用 Ajax 获取错误响应
【发布时间】:2016-03-07 15:26:20
【问题描述】:

目前我使用@skube 方法提交mailchimp 表单。我可以成功设置并且它确实有效。但我想在我的站点中获取 mailchimp 错误消息。

喜欢:虽然有人已经订阅或提供了虚假电子邮件,或 mailchimp 提供的无效电子邮件等。

这是@skube提供的代码。

html代码:

<form class="myform" action="http://XXXXXXXXXlist-manage2.com/subscribe/post" method="POST">
  <input type="hidden" name="u" value="XXXXXXXXXXXXXXXX">
  <input type="hidden" name="id" value="XXXXXXXXX">
  <input class="input" type="text" value="" name="MERGE1" placeholder="First Name" required>
  <input type="submit" value="Send" name="submit" id="mc-embedded-subscribe">
</form>

ajax 代码

$('.myform').submit(function(e) {
  var $this = $(this);
  $.ajax({
      type: "GET", // GET & url for json slightly different
      url: "http://XXXXXXXX.list-manage2.com/subscribe/post-json?c=?",
      data: $this.serialize(),
      dataType    : 'json',
      contentType: "application/json; charset=utf-8",
      error       : function(err) { alert("Could not connect to the registration server."); },
      success     : function(data) {
          if (data.result != "success") {
              // Something went wrong, parse data.msg string and display message
          } else {
              // It worked, so hide form and display thank-you message.
          }
      }
  });
  return false;
});

我对此进行了搜索,但没有找到任何有效的文档。我找到了一个 github 代码,但我无法使其工作。这是github link

希望有人帮助我。

注意:我尝试使用存在的旧 api 代码方法,但 mailchimp 建议不要使用它。他们已经发布了 3.0 版本的 API

【问题讨论】:

标签: jquery ajax mailchimp


【解决方案1】:

很抱歉,感谢大家的时间。我得到了解决方案。解决方法真的很简单!

需要在错误部分使用data['msg']

所以代码看起来像:

$('.myform').submit(function(e) {
  var $this = $(this);
  $.ajax({
      type: "GET", // GET & url for json slightly different
      url: "http://XXXXXXXX.list-manage2.com/subscribe/post-json?c=?",
      data: $this.serialize(),
      dataType    : 'json',
      contentType: "application/json; charset=utf-8",
      error       : function(err) { alert("Could not connect to the registration server."); },
      success     : function(data) {
          if (data.result != "success") {
              // Something went wrong, parse data.msg string and display message

              alert(data['msg']);

          } else {
              // It worked, so hide form and display thank-you message.

              alert('Thanks for subscribe');
          }
      }
  });
  return false;
});

因此,您可以在 div 或 p 或任何想要显示输出的地方使用 data['msg']

例子:

$('#error').html(data['msg']);

谢谢大家。

【讨论】:

    【解决方案2】:

    感谢您的自动回复!

    对我有用,但我必须进行一些更改: 形式:

    <form id="mc-embedded-subscribe-form" name="mc-embedded-subscribe-form" class="validate myform" validate>
    

    ,没有动作和方法。

    ajax 调用:

    $.ajax({
                  type: "post",
                  url: "xxxxxx/subscribe/post-json?c=?",
                  data: $this.serialize(),
                  mode: 'no-cors',
                  dataType    : 'json',
                  contentType: "application/json",
                  error       : function(err) { alert("Could not connect to the registration server."); },
                  success     : function(data) {
                      if (data.result != "success") {
                          // Something went wrong, parse data.msg string and display message
    alert(data['msg']);
                      } else {
                          
    alert("thanks!");
                      }
                  }
              });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 2010-12-10
      • 2016-10-29
      • 1970-01-01
      • 2012-02-22
      • 2013-10-24
      相关资源
      最近更新 更多