weixin_33725239 2016-12-06 22:37 采纳率: 0%
浏览 55

关于Ajax JavaScript的错误

When I click on the button the data are sent, no problem with that but I don't see the success message and when I look the console, it write this error.

SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data    mailchimp_v3.js:100:16

The error is in relation with this line on the javascript

data = jQuery.parseJSON(response);

How to resolve this error and to have the message success.

Thank you.

the form to send information

       <?php  echo HTML::form('mailchimp', OSCOM::link('ext/api/mailchimp_v3/subscribe.php'), 'post', 'novalidate data-mailchimp="' . $mailchimp_list_anonymous . '" data-redirect="'. OSCOM::link('index.php') .'"'); ?>
              <div class="col-md-12">
                <?php echo HTML::inputField('email', '', 'required  id="email" placeholder="' . OSCOM::getDef('entry_email_address') . '" email data-validation-email-message="' . OSCOM::getDef('enter_valid_email_address') . '" required data-validation-required-message="' . OSCOM::getDef('enter_email_address') . '""', 'email'); ?>
              </div>
              <div class="col-md-12">
                <?php echo HTML::button(OSCOM::getDef('button_submit'), 'fa fa-send', null, 'info',null,null); ?>
              </div>
<?php
            echo '</form>';
?>

the script

$(document).ready(function(){

  var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

  function validateField(element,name,value){

    var required = element.attr('required');
    var email = element.attr('email');

    if(typeof required !== typeof undefined && required !== false){

      if(value.length == 0){
        element.addClass('error');
        element.after('<span class="field-error">' + element.attr('data-validation-required-message') + '</span>')

        return false;
      }

    }

    if(typeof email !== typeof undefined && email !== false){

      if(!re.test(value)){
        valid = false;

        element.addClass('error');
        element.after('<span class="field-error">' + element.attr('data-validation-email-message') + '</span>')

        return false;
      }

    }

    return true;

  }

  $('form[data-mailchimp]').each(function(){

    var form = $(this);
    var list = form.attr('data-mailchimp');
    var url = form.attr('action');
    var redirect = form.attr('data-redirect');

    function success(){
      if(typeof redirect !== typeof undefined && redirect !== false){

        window.location = redirect;

      }
    }


    if(!list) return;

    form.submit(function(e) {

      e.preventDefault();

      //Reset errors and such
      form.removeClass('loading');
      form.find('.field-error').remove();
      form.find('.form-error').html('');

      var fields = form.serializeArray();


      var data = {
        list : list,
        email : "",
        timestamp_signup : (new Date).getTime(),
        merge_fields : {},
        interests : {}
      }


//Validate all of the fields
      for(var i = 0; i < fields.length; i++){
        var field = fields[i];
        if(!validateField(form.find('[name='+field.name+']'),field.name,field.value)) return;

        if(field.name.indexOf('email') > -1){
          data.email = field.value;
        }
      }


      //All good
      form.addClass('loading');

      if(data.email == ""){

        form.find('.form-error').html('Please provide a valid email address.');

        return;
      }

      $.post(url, data, function(response) {

        data = jQuery.parseJSON(response);

        form.removeClass('loading');


//Member already exists
        if(data.status != null && data.status == 400 && data.title == "Member Exists"){
          form.addClass('success');
          success();
          console.log('member exists already');
        }
        else if(data.id && data.id.length > 0){
          form.addClass('success');
          success();
          console.log('member added');
        }
        else{
          form.find('.form-error').html('There was an error submitting the form.');
        }
      });
    });
  });
});

More information

<anonyme> http://...../mailchimp_v3.js:100:16
    r.Callbacks/i https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:27978
    r.Callbacks/j.fireWith https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:28749
    A https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:4:14201
    .send/c/< https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:4:16491

Foolow to comment data = response; seems correct, and I have no error, but I don't see the message : success

  • 写回答

2条回答 默认 最新

  • weixin_33717298 2016-12-06 22:42
    关注

    It could be either of the 2 things.

    • Not a valid JSON (maybe invalid JSON string)
    • Your response is already a JSON, and you are trying to parse it again. Get rid of data = jQuery.parseJSON(response); and try using the response as is.

    Simplest thing to do is to console.log(response) to see what the response is.

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择