Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AJAX无法从PHP读取回显数据?_Javascript_Php_Ajax_Echo - Fatal编程技术网

Javascript AJAX无法从PHP读取回显数据?

Javascript AJAX无法从PHP读取回显数据?,javascript,php,ajax,echo,Javascript,Php,Ajax,Echo,我一直在测试我的寄存器,在某种程度上代码是有效的。但它似乎无法比较PHP脚本中的文本 我尝试使用现有的电子邮件和用户名,现有的电子邮件,现有的用户名和两个不存在的用户名和电子邮件。但是所有这些都给我回显数据+Fail(这是JQuery中的end条件语句) JQuery: $( function ajaxRegCheck() { $('#reg_form').submit(function(e) { e.preventDefault(); $.ajax({

我一直在测试我的寄存器,在某种程度上代码是有效的。但它似乎无法比较PHP脚本中的文本

我尝试使用现有的电子邮件和用户名,现有的电子邮件,现有的用户名和两个不存在的用户名和电子邮件。但是所有这些都给我回显数据+Fail(这是JQuery中的end条件语句)

JQuery:

$( function ajaxRegCheck() {
    $('#reg_form').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'save_register.php',
            data: $(this).serialize(),
            success: function(data)
            {

                if(data == "un_em_exists"){
                    alert(data+" Username and Email exists.");
                } else if(data == "un_exists") {
                    alert(data+" Username exists.");
                } else if(data == "em_exists") {
                    alert(data+" Email exists.");
                } else if(data == "success"){
                    alert(data+" Created account.");
                } else {
                    alert(data+ " Fail.");
                }
            } 
        });
    });
});
if(($exist_check_user->rowCount() > 0) AND ($exist_check_em->rowCount() > 0)){
    echo "un_em_exists";
} else if($exist_check_user->rowCount() > 0) {
    echo "un_exists";
} else if($exist_check_em->rowCount() > 0) {
    echo "em_exists";
} else {
    $sql = "INSERT INTO Users (username, email, password) VALUES (:r_un, :r_em, :r_pw)";
    $q = $cdb->prepare($sql);
    $q->execute(array(':r_un'=>$reg_username,':r_em'=>$reg_email,':r_pw'=>$reg_pw));
    echo "success";
}
PHP寄存器:

$( function ajaxRegCheck() {
    $('#reg_form').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'save_register.php',
            data: $(this).serialize(),
            success: function(data)
            {

                if(data == "un_em_exists"){
                    alert(data+" Username and Email exists.");
                } else if(data == "un_exists") {
                    alert(data+" Username exists.");
                } else if(data == "em_exists") {
                    alert(data+" Email exists.");
                } else if(data == "success"){
                    alert(data+" Created account.");
                } else {
                    alert(data+ " Fail.");
                }
            } 
        });
    });
});
if(($exist_check_user->rowCount() > 0) AND ($exist_check_em->rowCount() > 0)){
    echo "un_em_exists";
} else if($exist_check_user->rowCount() > 0) {
    echo "un_exists";
} else if($exist_check_em->rowCount() > 0) {
    echo "em_exists";
} else {
    $sql = "INSERT INTO Users (username, email, password) VALUES (:r_un, :r_em, :r_pw)";
    $q = $cdb->prepare($sql);
    $q->execute(array(':r_un'=>$reg_username,':r_em'=>$reg_email,':r_pw'=>$reg_pw));
    echo "success";
}
我不知道为什么它会跳过JQuery中的if语句,直接转到最后一个else条件。它显然正确地从PHP端输出字符串,但似乎无法比较字符串

例如,如果我使用现有用户名和电子邮件注册,它将回显“un_em_exists”响应。在JQuery上,警报与此匹配,因为它说,“un_em_exists Fail.”,这是最后一条语句

更新:

发现问题,但不太确定如何修复。我测试了字符串长度,作为回报,我得到了字符串长度16(从JQuery端测试),在PHP上得到了12——这是“un_em_exists”的正确数量


我不知道额外的4个金额来自何处。

请确保返回的值是文本,如下所示:

$( function ajaxRegCheck() {
    $('#reg_form').submit(function(e) {
        e.preventDefault();
        $.ajax({
            dataType: 'text',
            type: "POST",
            url: 'save_register.php',
            data: $(this).serialize(),
            success: function(data)
            {

                if(data == "un_em_exists"){
                    alert(data+" Username and Email exists.");
                } else if(data == "un_exists") {
                    alert(data+" Username exists.");
                } else if(data == "em_exists") {
                    alert(data+" Email exists.");
                } else if(data == "success"){
                    alert(data+" Created account.");
                } else {
                    alert(data+ " Fail.");
                }
            } 
        });
    });
});

成功使用data.trim()修剪数据后,可能会出现空白

是否检查了字符串长度?也就是说,可能会有额外的新行或其他空白。在PHP中发送代码并在客户端转换和获取其字符串值会更容易。检查Ajax调用期间返回的
data
的数据类型,您可能会返回一个对象而不是字符串,这将导致所有字符串比较都为false。无法确认。数据类型为字符串。我使用了:alert(jQuery.type(data)+“Fail”);使用json_编码如下内容。刚刚添加了这个和下面关于退出的评论;还是一样,谢谢!我不认为这是答案,因为从肉眼看,它看起来是一样的。但在我测试了这两个字符串长度之后,我意识到有一个问题,因为它产生了16个字符而不是12个字符。我不知道额外的4个字符是从哪里来的。