php代码没有重定向到ajax调用

时间:2016-06-16 11:54:08

标签: javascript php jquery ajax

我有以下js代码,它正在验证在后端调用php脚本的表单:

$(function() {

    // Setup form validation on the #register-form element
    $("#register_form").validate({

        // Specify the validation rules
        rules: {
            register_username: "required",
            register_password: "required",
            register_email: {
                required: true,
                register_email: true
            },
            register_confirm_password: {
                required: true,
                minlength: 5
            },
        },

        // Specify the validation error messages
        messages: {
            register_username: "Please enter your username",
            register_password: "Please enter your password",
            register_confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            register_email: "Please enter a valid email address",
        },

       submitHandler(function(form) {
            $.ajax({  
              type: 'POST',
              url: $(this).attr('action'),
              data: $(this).serialize(),
              dataType : 'json',
              success(function(data) {
                if (data){
                    alert("success");
                    // $(this)[0].reset();
                }
              })
            });
            return false;
        });
    });

});

在后端我有以下代码:

var_dump($_POST);

问题是在浏览器中,我成功地能够转储php数据,但无法在js中调用此函数消息:

submitHandler(function(form) {
    $.ajax({  
        type: 'POST',
        url: $(this).attr('action'),
        data: $(this).serialize(),
        dataType : 'json',
        success(function(data) {
            if (data) {
                alert("success");
                // $(this)[0].reset();
            }
        })
    });
    return false;
});

请帮我解决上面的代码。

1 个答案:

答案 0 :(得分:2)

问题是var_dump($_POST);不会输出有效的JSON。将其更改为:

echo json_encode($_POST);