简体   繁体   English

JSON 中位于位置 0 的意外 < 标记

[英]Unexpected < token in JSON at position 0

I keep receiving this JSON error,i think it has something to do with html tags or any other tags that conflicts with it.我不断收到这个 JSON 错误,我认为它与 html 标签或与它冲突的任何其他标签有关。

Here is my PHP Code:这是我的 PHP 代码:

 <?php
    require_once("connection.php");

    class AddAdminUPController extends Connection{

        public function addAdminUP(){
            include "function.php";
            $username = cleanData($_POST['username']);
            $password = cleanData($_POST['password']);
            if (!empty($username)){
                if(!empty($password)){
                    if(strlen($password) > 8){
                        $select_query = "select * from sample_user where user_name = ?";
                        $stmt = $this->db->prepare($select_query);
                        $stmt->bindParam(1,$username);
                        if($stmt->execute()){
                            if($stmt->rowCount() <= 0){
                                $password = password_hash($password,PASSWORD_BCRYPT,array('cost' => 12));
                                $create_query = "insert into sample_user(user_name,password)values(?,?)";
                                $stmt = $this->db->prepare($create_query);
                                $stmt->bindParam(1,$username);
                                $stmt->bindParam(2,$password);
                                if($stmt->execute()){
                                    echo "<script>".
                                         "Materialize.toast('Created SuccessFully!', 5000, 'green')"
                                        ."</script>";
                                    echo "<script>".
                                         "$('#add_admin_up_form').hide();".
                                         "$('#add_admin_up').hide();";
                                    ?>

                                    $(".enrollment_title").html("<i class='material-icons'>fingerprint</i> Enroll Fingerprint");

                                    <?php
                                    echo "</script>";

                                    include 'flexcode_sdk/include/global.php';
                                    include 'flexcode_sdk/include/function.php';
                                    if(isset($_GET['action']) && $_GET['action'] == 'add'){
                                        $lastID = $this->db->lastInsertId();
                                    ?>

                                    <script type="text/javascript">

                                        $('title').html('User');
                                        function user_register(user_id, user_name) {

                                            $('body').ajaxMask();

                                            regStats = 0;
                                            regCt = -1;
                                            try
                                            {
                                                timer_register.stop();
                                            }
                                            catch(err)
                                            {
                                                console.log('Registration timer has been init');
                                            }

                                            var limit = 4;
                                            var ct = 1;
                                            var timeout = 5000;

                                            timer_register = $.timer(timeout, function() {
                                                console.log("'"+user_name+"' registration checking...");
                                                user_checkregister(user_id,$("#user_finger_"+user_id).html());
                                                if (ct>=limit || regStats==1)
                                                {
                                                    timer_register.stop();
                                                    console.log("'"+user_name+"' registration checking end");
                                                    if (ct>=limit && regStats==0)
                                                    {
                                                        alert("'"+user_name+"' registration fail!");
                                                        $('body').ajaxMask({ stop: true });
                                                    }
                                                    if (regStats==1)
                                                    {
                                                        $("#user_finger_"+user_id).html(regCt);
                                                        alert("'"+user_name+"' registration success!");
                                                        $('body').ajaxMask({ stop: true });
                                                        load('view_admin.php?action=add');
                                                    }
                                                }
                                                ct++;
                                            });
                                        }

                                        function user_checkregister(user_id, current) {
                                            $.ajax({
                                                url         :   "view_admin.php?action=checkreg&user_id="+user_id+"&current="+current,
                                                type        :   "GET",
                                                success     :   function(data)
                                                                {
                                                                    try
                                                                    {
                                                                        var res = jQuery.parseJSON(data);
                                                                        if (res.result)
                                                                        {
                                                                            regStats = 1;
                                                                            $.each(res, function(key, value){
                                                                                if (key=='current')
                                                                                {
                                                                                    regCt = value;
                                                                                }
                                                                            });
                                                                        }
                                                                    }
                                                                    catch(err)
                                                                    {
                                                                        alert(err.message);
                                                                    }
                                                                }
                                            });
                                        }

                                    </script>

                                    <?php

                                        $last_id_query = "select * from sample_user where user_id = ?";
                                        $stmt = $this->db->prepare($last_id_query);
                                        $stmt->bindParam(1,$lastID);
                                        if ($stmt->execute()){
                                            while($row = $stmt->fetch(PDO::FETCH_OBJ)){

                                                $url_register =
                                                base64_encode($base_path."register.php?user_id=".$row->user_id);
                                                echo "<br><a href='finspot:FingerspotReg;$url_register' onclick=\"user_register('".$row->user_id."','".$row->user_name."')\" class='fw_button general_button btn waves-effect waves-light'>Register Fingerprint</a>";
                                            }
                                        }
                                    }
                                    elseif (isset ($_GET['action']) && $_GET['action'] == 'checkreg') {

                                            $sql1       = "SELECT count(finger_id) as ct FROM sample_finger WHERE user_id=".$_GET['user_id'];
                                            $result1    = mysql_query($sql1);
                                            $data1      = mysql_fetch_array($result1);
                                            if (intval($data1['ct']) > intval($_GET['current'])) {
                                                $res['result'] = true;
                                                $res['current'] = intval($data1['ct']);
                                            }
                                            else
                                            {
                                                $res['result'] = false;
                                            }
                                            echo json_encode($res);
                                    }
                                    else { echo "Parameter invalid..";}
                                }
                                else{
                                    echo "<script>".
                                         "Materialize.toast('Query Failed!', 5000, 'red')"
                                        ."</script>";
                                }
                            }
                            else{
                                echo "<script>".
                                     "Materialize.toast('Username already exists!', 5000, 'red')"
                                    ."</script>";
                            }
                        }
                        else{
                            echo "<script>".
                                 "Materialize.toast('Query Failed!', 5000, 'red')"
                                ."</script>";
                        }
                    }
                    else{
                        echo "<script>".
                             "Materialize.toast('Password is too short!', 5000, 'red')"
                            ."</script>";
                    }
                }
                else{
                    echo "<script>".
                         "Materialize.toast('Password is empty!', 5000, 'red')"
                        ."</script>";
                }
            }
            else{
                echo "<script>".
                     "Materialize.toast('Username is empty!', 5000, 'red')"
                    ."</script>";
            }

        }
    }
$add_admin_up_controller = new AddAdminUPController;
echo $add_admin_up_controller->addAdminUP();

?>

I need help in fixing this kind of error.我需要帮助来解决这种错误。 It keeps displaying:一直显示:

SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse () at Function.jQuery.parseJSON (jquery.js:8520) at Object.success (eval at (jquery.js:339), :55:90) at fire (jquery.js:3148) at Object.fireWith [as resolveWith] (jquery.js:3260) at done (jquery.js:9314) at XMLHttpRequest.callback (jquery.js:9718) SyntaxError:在 Object.success 的 Function.jQuery.parseJSON (jquery.js:8520) 的 JSON.parse () 的位置 0 处的 JSON 中的意外标记 < 在在 XMLHttpRequest.callback (jquery.js:9718) 完成 (jquery.js:9314) 在 Object.fireWith [as resolveWith] (jquery.js:3260) 触发 (jquery.js:3148)

You are returning plain text instead of JSON.您正在返回纯文本而不是 JSON。 In your ajax call put the response dataType: 'json' and the part of PHP:在您的 ajax 调用中放入响应 dataType: 'json' 和 PHP 的一部分:

header("Content-Type: application/json");
echo json_encode($res);
exit;

This error due to your responce is not in json format and your ajax request is expecting json responce .由于您的响应不是 json 格式而导致的此错误,并且您的 ajax 请求需要 json 响应。 You can change responce header by use dataType : 'text/html' in your ajax request.您可以通过在 ajax 请求中使用 dataType : 'text/html' 来更改响应标头。

Here in action .在这里行动。

function user_checkregister(user_id, current) {
$.ajax({
    url         :   "view_admin.php?action=checkreg&user_id="+user_id+"&current="+current,
    type        :   "GET",
    dataType: 'text/html',
    success     :   function(data)
                    {
                        try
                        {
                            var res = jQuery.parseJSON(data);
                            if (res.result)
                            {
                                regStats = 1;
                                $.each(res, function(key, value){
                                    if (key=='current')
                                    {
                                        regCt = value;
                                    }
                                });
                            }
                        }
                        catch(err)
                        {
                            alert(err.message);
                        }
                    }
});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM