Ajax消息未显示

时间:2016-12-10 14:09:49

标签: javascript jquery ajax mailchimp

我尝试显示错误消息,但它无法正常工作。

当电子邮件不存在时, 我收到此消息:谢谢:您的电子邮件已添加

但如果电子邮件总是在mailchimp或其他错误中,我在按钮提交下没有留言。

你有想法解决这一点吗?

谢谢

错误的详细信息来自服务器。

string(88) "400: ****** is already a list member. Use PUT to insert or update list members."
array(5) {
  ["type"]=>
  string(77) "http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/"
  ["title"]=>
  string(13) "Member Exists"
  ["status"]=>
  int(400)
  ["detail"]=>
  string(83) "********* is already a list member. Use PUT to insert or update list members."
  ["instance"]=>
  string(0) ""
}

我的javascript是

          $footer .= '
    <script>
      $(document).ready(function() {
        $('#signup').submit(function() {
    $("#message").html("<div class=\"alert alert-info\" style=\"padding:05px; margin-top:5px;\" role=\"alert\">Thank you : your email is added</div>");
    $.ajax({
      url: 'ext/api/mailchimp_v3/subscribe.php', // proper url to your "store-address.php" file
      type: 'POST', // <- IMPORTANT
      dataType: 'json',
      data: $('#signup').serialize() + '&ajax=true',
      success: function(msg) {
        var message = '';
        var result = '';
        message = $.parseJSON(msg);
        if (message.status === 'pending') { // success
          result = '<div class=\"alert alert-success\" role=\"alert\">Thank you: an email has sent you for confirmation</div>';
        } else { // error
          result = '<div class=\"alert alert-danger\" role=\"alert\">Error ' + message.detail + '</div>';
        }
      },
      complete: function(message) {
        $('#message').html('<div> ' + message.title + '</div>'); // display the message
      }
    });
    $('#fname').attr('value',''); // reset input field
    $('#lname').attr('value',''); // reset input field
    $('#email').attr('value',''); // reset input field
    return false;
  });
});
    </script>';

PHP代码

      if ( isset($_POST['anonymous'])) {
        $list_id = MODULES_HEADER_TAGS_MAILCHIMP_LIST_ANONYMOUS;
        $merge_vars = [
          'FNAME' => '',
          'LNAME' => ''
        ];

      } else {
        $list_id = MODULES_HEADER_TAGS_MAILCHIMP_LIST_CUSTOMERS;

        $merge_vars = [
          'FNAME' => $_POST['firstname'],
          'LNAME' => $_POST['lastname']
        ];
      }

      $array = [
                  'email_address' => $_POST['email'],
                  'merge_fields'  => $merge_vars,
                  'status'        => MODULES_HEADER_TAGS_MAILCHIMP_STATUS_CHOICE
                ];


      if (MODULES_HEADER_TAGS_MAILCHIMP_STATUS_CHOICE == 'pending') {
        $status = 'pending';
      } else {
        $status = 'subscribed';
      }

      $mc = new \MailChimp($key);

    // add the email to your list
       $result = $mc->post('/lists/' . $list_id . '/members', $array);
//send
        json_encode($result);

// If being called via ajax, run the function, else fail - console
      if ( MODULES_HEADER_TAGS_MAILCHIMP_DEBUG == 'True') {

        if ($_POST['ajax']) {
          var_dump($result); // send the response back
        } else {
          var_dump('Method not allowed - please ensure JavaScript is enabled in this browser');
        }
      }

0 个答案:

没有答案