Jquery错误:未捕获TypeError:无法读取属性' length'未定义的

时间:2014-12-10 08:59:08

标签: php jquery ajax

我有一个表单来调用ajax进行后端处理,所有输入都将存储在表中并返回'成功'要通知用户提交成功。但我在回调期间面临一个问题,输入数据可以保存到表中,但回调停止时出现错误,我不知道出了什么问题,同一个脚本应用于另一个表单(具有不同的表单字段)工作正常。

控制台日志 - Chrome:

Uncaught TypeError: Cannot read property 'length' of undefined
m.extend.each
$.ajax.success
j
k.fireWith
x
b

火狐

TypeError: a is undefined

...rCase()},each:function(a,b,c){var d,e=0,f=a.length,g=r(a);if(c){if(g){for(;f>e;e...

firefox错误指向f=a

脚本:

$(document).ready(function() {

        $("#status").hide();

        $('#btn_submit').click(function(){
            var params = $('#project_form').serialize();
            var btn = $(this);
            btn.button('loading')

            $.ajax({
                url: baseurl + '/process_form.php',
                type: 'POST',
                data: params,
                dataType: 'json',
                success: function(response){
                    if(response.success == 'success'){

                        $('#status').html('<b>Thank you</b>').show();

                        $('html,body').animate({
                            scrollTop: $('#top').offset().top
                        },500);

                    }else{
                        $('[id$="_error"]').html('');
                        $.each(response.error, function(key, value){
                            if(value){
                                $('#' + key + '_error').html(value);
                            }
                        });
                    }
                },
                error: function(){
                    console.log(arguments);
                }
            }).always(function(){
                btn.button('reset')
            });

        });

});

process_form.php

header("Content-Type: application/json");

$serialized_data = serialize($_POST);

$fullname = $mysqli->real_escape_string(ucwords(strtolower($_POST['fullname'])));
$age = $mysqli->real_escape_string(ucwords(strtolower($_POST['age'])));
$email = $mysqli->real_escape_string(strtolower(trim($_POST['semail'])));
$phone = $mysqli->real_escape_string(trim($_POST['phone']));
$proj_id = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_POST['project_id']));
$proj_name = $mysqli->real_escape_string(ucwords(strtolower($_POST['project_name'])));
$agent_id = preg_replace('/[^0-9]/', '', $mysqli->real_escape_string($_POST['agent_id']));
$agent_email = $mysqli->real_escape_string(strtolower(trim($_POST['agent_email'])));
$agent_name = $mysqli->real_escape_string(ucwords(strtolower($_POST['agent_name'])));
$captcha = $_POST['captcha_code'];

$func = new Functions();
$message = array();

// validate
if(empty($fullname) || strlen($fullname) < 2){
    $message['error']['fullname'] = 'Your name is required';
}
if(empty($age)){
    $message['error']['age'] = 'Your age is required';
}
if(!$func->check_email_address($email)){
    $message['error']['semail']  = 'Invalid email address';
}
if(empty($phone) || strlen($phone) < 10){
    $message['error']['phone'] = 'Phone num is required';
}

$securimage = new Securimage();
$securecode = $securimage->getCode();
if(strtolower($securecode) != strtolower($captcha)) {
    $message['error']['captcha'] = 'Incorrect security code';
}


if(!isset($message['error'])){

    // insert table
    $create = $mysqli->query("INSERT INTO `xxx` ...") or die($mysqli->error);

    if($create){
        //send email
        $to = xxx;
        $subject = 'yyy';
        $msg = '<html>
                    <body>
                        blah...
                    </body>
                </html>';
        $from = zzz;
        $headers  = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= 'From: '.$fullname.' <'.$from.'>' . "\r\n";

        if(mail($to, $subject, $msg, $headers)){
            $message['success'] = 'success';
        }
    }

}

echo json_encode($message);

2 个答案:

答案 0 :(得分:0)

改变这个:

 if(response.success == 'success'){

到此:

 if(response[0].success == 'success'){

在我看来,你有一个对象 [{}] 的数组,所以response.success不可用,而是你可以像上面的答案中所建议的那样传递索引response[0].success

答案 1 :(得分:0)

我认为这是因为mail功能失败,并在此处返回false

if(mail($to, $subject, $msg, $headers)){
   $message['success'] = 'success';
}

因此您的回复既没有success属性,也没有error属性,这会导致错误:

$.each(response.error, function(key, value){
   if(value){
      $('#' + key + '_error').html(value);
   }
});

您可以尝试以下内容:

if(mail($to, $subject, $msg, $headers)){
    $message['success'] = 'success';
}else{
    $message['error']['mail'] = 'Mail error!';
}

或在将数据保存到表中后直接设置$message['success'] = 'success';,无论邮件是否成功。