为什么我会遇到未捕获的错误:语法错误,JavaScript中无法识别的表达式?

时间:2018-07-20 02:56:26

标签: javascript php jquery

我正在使用WordPress中的高级自定义字段来构建自定义的多步注册表单,我已经布局了表单,但是由于以下错误我被困在jQuery验证中:

  

未捕获的错误:语法错误,无法识别的表达式:糟糕,出了点问题,请稍后重试。

我找不到我的代码出了什么问题。

    var epmformdata = {};

    $.each($('#field-group-1 input, #field-group-1 select'), function(i, v) {
        if (v.type !== 'submit') {
            epmformdata[v.name] = v.value;
        }
    }); //end each

    // Do AJAX request
    $.ajax({
        dataType: 'json',
        type: 'POST',
        url: ajaxurl,
        data:{
            action: 'epm_registration_process',
            epformdata: epmformdata
        },
        success: function(response) {
            if (response === true) {
                // If user is created

                if(animating) return false;
                animating = true;

                current_fs = $(this).parent();
                next_fs = $(this).parent().next();

                //activate next step on progressbar using the index of next_fs
                $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

                //show the next fieldset
                next_fs.show(); 
                //hide the current fieldset with style
                current_fs.animate({opacity: 0}, {
                    step: function(now, mx) {
                        //as the opacity of current_fs reduces to 0 - stored in "now"
                        //1. scale current_fs down to 80%
                        scale = 0.8 + (1 - now) * 0.2;
                        //2. bring next_fs from the right(50%)
                        left = (now * 50)+"%";
                        //3. increase opacity of next_fs to 1 as it moves in
                        opacity = 1 - now;
                        /*current_fs.css({
                            'transform': 'scale('+scale+')',
                            'position': 'absolute'
                        });*/
                        next_fs.css({'opacity': opacity});
                    }, 
                    duration: 0, 
                    complete: function(){
                        current_fs.hide();
                        animating = false;
                    }, 
                    //this comes from the custom easing plugin
                    easing: 'easeInOutBack'
                });
            } else {
                $.each(response, function(i, v) {
                    console.log(i + " => " + v); // view in console for error messages
                    var msg = '<label class="error" for="'+i+'">'+v+'</label>';
                    $('input[name="' + i + '"], select[name="' + i + '"]').addClass('inputTxtError').after(msg);
                });
                var keys = Object.keys(response);
                $('input[name="'+keys[0]+'"]').focus();

                $('.result-message').html( response ); // If there was an error, display it in results div
                $('.result-message').addClass('alert-danger'); // Add class failed to results div
                $('.result-message').show(); // Show results div
            }
        },
        error: function(errorThrown) {
            console.log(errorThrown);
        }
    });

我的PHP

// Verify nonce
if( !isset( $_POST['nonce'] ) || !wp_verify_nonce( $_POST['nonce'], 'epm_new_user' ) )
die( 'Ooops, something went wrong, please try again later.' );

//Prepare basic user info
$username = strtolower($_POST['acf']['field_5b4be98fd288c']);
$firstname = $_POST['acf']['field_5b4be9d8d288d'];
$lastname = $_POST['acf']['field_5b4be9e8d288e'];
$email = $_POST['acf']['field_5b4be9f7d288f'];
$password = $_POST['acf']['field_5b4bea0bd2890'];
$cpassword = $_POST['acf']['field_5b4bea8bd2891'];

$error = array();
//IMPORTANT: You should make server side validation here!
if( empty($username) ){
    $_SESSION['errors']['acf']['field_5b4be98fd288c'] = "Please enter a username";
}
if( empty($firstname) ){
    $_SESSION['errors']['acf']['field_5b4be9d8d288d'] = "Please enter your first";
}

if(count($_SESSION['errors']) > 0){
    //This is for ajax requests:
    if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) &&  strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
        echo json_encode($_SESSION['errors']);
        exit();
    }
    //This is when Javascript is turned off:
        echo '<ul>';
        foreach($_SESSION['errors'] as $key => $value){
            echo '<li>' . $value . '</li>';
        }
         echo '</ul>';
        exit();
}else{
//form validation successful - process data here!!!!

    //Register user
    $user_id = wp_insert_user( 
        /*array(
            'user_login'    =>  $username,
            'first_name'    =>  $firstname,
            'last_name'     =>  $lastname,
            'display_name' => $firstname . ' ' . $lastname,
            'nickname' => strtolower($firstname . ' ' . $lastname),
            'user_email'    =>  $email,
            'user_pass'     =>  $password,  // When creating an user, `user_pass` is expected.
            'role' => 'employer',
        )*/
    );

    // Return
    if( !is_wp_error($user_id) ) {
        echo '1';
    }
    else {
        echo $user_id->get_error_message();
    }
    exit();
}   

1 个答案:

答案 0 :(得分:0)

在调用服务器端代码时,应将有效的随机数传递给随机数参数。

例如,假设您已使用wp_enqueue_script函数将代码实现到要包含的JavaScript文件中

wp_enqueue_script( 'my_js_file', 'http://www.yourwebsitedomain.com/your_js_file.js');

由于必须在服务器端生成随机数,因此,如果将JavaScript文件加入队列,则应调用wp_localize_script函数以使用随机数生成全局JavaScript变量(我将其称为全局变量my_global_var):

wp_enqueue_script( 'my_js_file', 'http://www.yourwebsitedomain.com');
wp_localize_script( 'my_js_file', 'my_global_var', array('nonce'=> wp_create_nonce('epm_new_user')));

现在,在您的JavaScript代码中,您应该编辑一段代码:

data:{
        action: 'epm_registration_process',
        epformdata: epmformdata
    }

如下:

data:{
        action: 'epm_registration_process',
        nonce: my_global_var['nonce'],
        epformdata: epmformdata
    }

仅此而已。