Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.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调用中获取parsererror_Javascript_Php_Jquery_Ajax - Fatal编程技术网

Javascript 在Ajax调用中获取parsererror

Javascript 在Ajax调用中获取parsererror,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我试图用php、jquery、ajax、json创建一个登录系统,它会验证是否有空字段,但一旦我完成并提交表单,ajax调用失败,控制台就会在响应文本中显示json数组,所以问题不在php部分,但它说parsererror,我不知道如何解决这个问题,我还是个新手。这是我的密码: index.php validator.php log.php conexion.php ajax.js 我在控制台中遇到以下错误: js:53:Object{readyState:4,responseText: {fa

我试图用php、jquery、ajax、json创建一个登录系统,它会验证是否有空字段,但一旦我完成并提交表单,ajax调用失败,控制台就会在响应文本中显示json数组,所以问题不在php部分,但它说parsererror,我不知道如何解决这个问题,我还是个新手。这是我的密码:

index.php validator.php log.php conexion.php ajax.js 我在控制台中遇到以下错误:

js:53:Object{readyState:4,responseText: {fail:true,消息:El usuario aa不存在用户aa不存在},状态: 200,statusText:OK}ajax.js:54:parsererror

我从控制台得到这个:

SyntaxError:意外标记 在Object.parse native 在n.json 在zb 在z 在XMLHttpRequest


我似乎收到了一个格式不正确的请求,这是我在另一个问题中读到的

看起来您在ajax.js的第54行遇到了错误回调和解析错误,您有:

console.logtextStatus

我们需要进入你的成功回调。返回到浏览器的HTTP状态代码是什么?您可以使用Chrome工具或Firebug并在浏览器中查看响应来查找此信息

您的响应JSON正文如下所示: { 失败:是的, 消息:El usuario aa不存在用户aa不存在 }

包含El usuario aa no existe用户aa不存在的消息-这实际上是一个没有打开的尾随吗


重要提示:看起来您正在从请求值获取原始用户输入,并在以下语句中通过数据库运行:Select*from user where username='{$user}'。这让你有机会接触到所谓的。恶意攻击者可以利用此功能从数据库中删除数据或选择不希望公开的数据。您应该切换到使用或转义输入数据以防止出现这种情况。

确切的错误消息是什么?仅此parsererror,控制台显示带有验证消息的json数组,但它们不会显示在屏幕上,只有空字段验证正常工作。我们对确切的定义不同,复制并粘贴它。ajax.js:53:Object{readyState:4,responseText:{fail:true,messages:El-usuario aa不存在用户aa不存在},状态:200,状态文本:OK}ajax.js:54:parserror这是我在控制台中得到的结果。你有意使用跨域:true?也许可以试试数据:JSON.stringifydatayes,我遇到了错误部分,http状态是200,在$message中div有它的开始和结束标记,我不知道为什么开始没有出现,但我去掉了div标记,得到了相同的结果,parserror,你是对的,我只是在测试这个,我将使用pdo,准备好的语句、绑定参数等等,但这只是一个用于调试目的的测试,您能否按如下方式更新您的ajax.js:并从控制台粘贴结果?成功回调ajax.js:19{error:true,字段:{user:Debes-Ingresar tu Nombre de Usuario,pass:Debes-Ingresar tu Contraseña}ajax.js:22错误回调ajax.js:23{readyState:4,responseText:{\fail\:true,\messages\:\El-usuario-asadasdasd-no-existe\},状态:200,状态文本:OK}基本上和我完成表单时一样,错误部分是triggeredOK。我现在明白了。在第二个场景中,它一定返回了无效的JSON。你有没有办法在Firebug或Chrome工具中查看原始响应,并截取响应体的屏幕截图?还有-你能抓取返回的所有标题吗?我如何上传截图?
<html>
    <head>
        <title></title>
        <link rel="stylesheet" href="/ajax/css/bootstrap.min.css"/>
        <link rel="stylesheet" href="/ajax/css/style.css"/>
        <meta charset="UTF-8">
    </head>
    <body>

        <section>
            <div class="container">
                <div class="row">
                    <div class="col-xs-12" id="titulo">
                        <h3>Ajax Login</h3>
                    </div>
                    <div class="col-xs-10 col-xs-offset-1">
                        <form id="formulario" action="php/validator.php" method="post">
                            <div class="col-xs-4 col-xs-offset-4" id="formPart">
                                <label for="user">Nombre de Usuario</label>
                                <input type="text" id="user" name="user" class="form-control">
                                <div class="user"></div>
                            </div>
                            <div class="col-xs-4 col-xs-offset-4" id="formPart">
                                <label for="pass">Nombre de Usuario</label>
                                <input type="password" id="pass" name="pass" class="form-control">
                                <div class="pass"></div>
                            </div>
                            <div class="col-xs-4 col-xs-offset-4" id="formPart">
                                <input type="submit" value="Iniciar Sesión" class="btn btn-success">
                            </div>
                        </form>
                        <div class="col-xs-4 col-xs-offset-4" id="response"></div>
                    </div>
                </div>
            </div>
        </section>


        <script src="/ajax/js/jquery2.js"></script>
        <script src="/ajax/js/bootstrap.min.js"></script>
        <script src="/ajax/js/ajax.js"></script>
        <script>

            $(document).ready(function(){

                setTimeout(function(){

                    $('#user').focus();

                }, 700);

            });

        </script>
    </body>
</html>
<?php 

if($_POST){

    $required = array('user', 'pass');
    $validation = array('user' => 'Debes Ingresar tu Nombre de Usuario', 'pass' => 'Debes Ingresar tu Contraseña');
    $error = array();
    $inputs = array();

    foreach($required as $key){ 
        if(array_key_exists($key, $_POST)){
            if(empty($_POST[$key])){
                $error[$key] = $validation[$key];
            }
            else{
                $inputs[$key] = $_POST[$key];
            }
        }
        else{
            $error[$key] = $validation[$key];
        }
    }

    if(!empty($error)){     
        $array = array('error' => true, 'fields' => $error);
    }
    else{
        $user = $inputs['user'];
        include 'log.php';
        if(user($user)){
            $message = '<div class="alert alert-success">Bienvenido '.$inputs['user'].'</div>';
            $array = array('success' => true, 'message' => $message);
        }
        else{
            $messages = '<div class="alert alert-success">El usuario '.$inputs['user'].' no existe</div>';
            $array = array('fail' => true, 'messages' => $messages);
        }
    }
    header('Content-Type: application/json');
    echo json_encode($array);
}
else{
    header("Location: ../index.php");
}
<?php

function user($user){

    include 'conexion.php';
    $conexion = conectar();
    mysqli_set_charset($conexion, "utf8");


    $query = "Select * from user where username = '{$user}'";
    $result = mysqli_query($conexion, $query);
    $total = mysqli_num_rows($result);


    if($total > 0){

        return true;

    }
    else{

        return false;

    }

}
<?php 


function conectar(){

    error_reporting(0);

    $server = 'localhost';
    $user_db = 'root';
    $pass_db = '';
    $db = 'user';


    $conexion = new mysqli($server, $user_db, $pass_db, $db);

    if($conexion->connect_errno){

        echo 'Error de Conexion. Código de Error: '.$conexion->connect_errno;

    }
    else{

        return $conexion;

    }

}
$('#formulario').on('submit', function(){

    $('#formulario .warn').remove();
    $('form input').removeClass('warning');

    var thisForm = $(this),
        url = thisForm.attr('action'),
        type = thisForm.attr('method'),
        data = thisForm.serializeArray();


        $.ajax({

            url: url,
            type: type,
            data: data,
            dataType:"json",
            crossDomain: true,
            success: function(response){

                if(response.error){

                    $.each(response.fields, function(index, value){

                        $('.' + index).hide().html('<span class="warn">' + value + '</span>').fadeIn(700);
                        $('#' + index).addClass('warning');

                    });

                }

                else if(response.success){

                    $('#formulario').fadeOut(700, function(){

                        $('#response').hide().html(response.message).fadeIn(700);

                    });

                }

                else{

                    $('#response').hide().html(response.messages).fadeIn(700);

                }



            },
            error: function(xhr, textStatus){

                console.log(xhr);
                console.log(textStatus);

            }

        });


    return false;

});