Php jQuery验证提交程序未显示响应

Php jQuery验证提交程序未显示响应,php,jquery,json,Php,Jquery,Json,我有一个jQuery验证脚本,除了事件处理程序之外,它可以很好地与其他程序一起工作。为了解决问题,我简化了这篇文章 jQuery submitHandler: function (form) { $.ajax({ type: $(form).attr("method"), url: $(form).attr("action"), data: $(

我有一个jQuery验证脚本,除了事件处理程序之外,它可以很好地与其他程序一起工作。为了解决问题,我简化了这篇文章

jQuery

submitHandler: function (form) {
                $.ajax({
                    type: $(form).attr("method"),
                    url: $(form).attr("action"),
                    data: $(form).serialize(),
                    dataType : "json"
                })
                .done(function (data) {
                    if (data.resp > 0 ) {
                        alert(data.message);                       
                    }
                });
                return false; // required to block normal submit since you used ajax
            },
 // rest of the validation below, truncated for clarity
.done(function (data) {
$("#user_add_dialog").dialog({
    autoOpen: false,
    modal: true,
    close: function (event, ui) {
    },
    title: "Add User",
    resizable: false,
    width: 500,
    height: "auto"
});
$("#user_add_dialog").html(data.message);
$("#user_add_dialog").dialog("open");
});

return false; // required to block normal submit since you used ajax
submitHandler成功地发布到我的PHP脚本,该脚本将用户添加到数据库中,然后回显json_encode()结果

PHP

<?php
    // All processing code above this line has been truncated for brevity

    if($rows == "1"){
        $resp = array("resp"=>1, "message"=>"Account created successfully. Waiting for user activation.");
    }else{
        $resp = array("resp"=>2, "message"=>"User account already exists.");
    }

    echo json_encode($resp);
?>
<?php
    // All processing code above this line has been truncated for brevity

    if($rows == "1"){
        $resp = array("message"=>"Account created successfully. Waiting for user activation.");
    }else{
        $resp = array("message"=>"User account already exists.");
    }

    echo json_encode($resp);
?>


正如你所看到的,这个想法很简单。使用正确的响应消息提醒用户。当我运行脚本时,用户帐户将成功添加到数据库中,但不会向用户显示任何警报。Chrome中的控制台没有显示错误,我遗漏了什么?

done()中的
数据变量是一个字符串。你必须把它转换成这样的物体

var response = $.parseJSON(data);

为了访问属性

很抱歉,我在前面的回答中遗漏了代码中的
数据类型:“json”


不管怎样,我试过你的代码,它是有效的。
警报显示消息。我想你在别的地方出错了。我认为这与您正在编码为json(PHP部分)的数组有关。您得到的响应不完整。尝试调试您的PHP并与AJAX分开测试页面,看看结果是什么

经过一些修补后,我能够以我想要的方式工作。这是更新后的代码

jQuery

submitHandler: function (form) {
                $.ajax({
                    type: $(form).attr("method"),
                    url: $(form).attr("action"),
                    data: $(form).serialize(),
                    dataType : "json"
                })
                .done(function (data) {
                    if (data.resp > 0 ) {
                        alert(data.message);                       
                    }
                });
                return false; // required to block normal submit since you used ajax
            },
 // rest of the validation below, truncated for clarity
.done(function (data) {
$("#user_add_dialog").dialog({
    autoOpen: false,
    modal: true,
    close: function (event, ui) {
    },
    title: "Add User",
    resizable: false,
    width: 500,
    height: "auto"
});
$("#user_add_dialog").html(data.message);
$("#user_add_dialog").dialog("open");
});

return false; // required to block normal submit since you used ajax
PHP

<?php
    // All processing code above this line has been truncated for brevity

    if($rows == "1"){
        $resp = array("resp"=>1, "message"=>"Account created successfully. Waiting for user activation.");
    }else{
        $resp = array("resp"=>2, "message"=>"User account already exists.");
    }

    echo json_encode($resp);
?>
<?php
    // All processing code above this line has been truncated for brevity

    if($rows == "1"){
        $resp = array("message"=>"Account created successfully. Waiting for user activation.");
    }else{
        $resp = array("message"=>"User account already exists.");
    }

    echo json_encode($resp);
?>

console记录数据并查看您得到的信息。{resp:“帐户创建成功。”}如果我在函数中简单地添加了警报(数据),则会弹出警报,但不是消息文本,而是说[object object],这会在Chrome的JavaScript控制台中生成一个“无法识别的令牌”,并且不会弹出警报窗口。在控制台中展开警报会发现它在var声明上抛出了一个“匿名函数”错误。我添加了一个console.log(data)语句来验证我是否得到了返回的JSON,我是:{resp:“Account Created Successfully.”