2015-12-03 103 views
2

目前我使用@skube方法提交mailchimp表單。我可以成功設置和真正的工作。但我想在我的網站中獲取mailchimp錯誤消息。MailChimp使用Ajax獲取錯誤響應

贊:雖然有人已經訂閱或假電子郵件提供,或無效的電子郵件等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

+1

查看此帖.. http://www.webylog.com/2015/12/02/mailchimp-integration-with-php/ –

+0

得到了解決方案。感謝您的努力和時間:) –

回答

2

抱歉並感謝大家的支持。我得到了解決方案。解決方案非常簡單!

需要使用data ['msg'] at error section。

所以代碼如下:

$('.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或任何地方..要顯示輸出。

例子:

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

謝謝大家。