简体   繁体   English

如何解决 Uncaught SyntaxError: Unexpected token < in JSON at position 0 控制台错误?

[英]How to solve Uncaught SyntaxError: Unexpected token < in JSON at position 0 console error?

I am very confused.我很困扰。 My forum was working fine yesterday and today then now I am getting this error and my forum no longer displays or works.我的论坛昨天和今天运行良好,然后现在我收到此错误,我的论坛不再显示或工作。 I have also got another js and php for posting a new comment.我还有另一个 js 和 php 用于发布新评论。 The comment is still being added to my database it is just not appearing.该评论仍在添加到我的数据库中,只是没有出现。 Anyone know why this is?有谁知道这是为什么? This is the full error I am getting:这是我得到的完整错误:

VM232:1 Uncaught SyntaxError: Unexpected token < in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.success (comments.js:13)
    at u (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at k (jquery.min.js:2)
    at XMLHttpRequest.<anonymous> (jquery.min.js:2)

HTML HTML

          <div id="content" class="content">
                <form id="forumPost" method='POST'>
                   <textarea rows="3" col="60" name="comment" placeholder="Create a Post..." id="comment"></textarea>
                   <button><input type='submit' name='submit' value='Post' class="post"></button>
                </form>
                <p id="error" class="errormessage"></p>
                <p id="allcomments" class="postmessage"></p>

                <div class="comment-container">
                    <div class="username"><!--obj.username--></div>
                    <div class="comment"><!--obj.comment--></div>
                    <div class="date"><!--obj.commDate--></div>
                    <div class="like"><!--obj.sentiment--></div>
                </div>
            </div>

JS JS

$(document).ready(function() {

     var comments = document.getElementById("allcomments").value; 

     //Get Storage 
                var username = window.localStorage.getItem("username");

        // Call Ajax for existing comments
        $.ajax({
        type: 'GET',
        url: 'comments.php',
        success: function(result) {
            var arr = JSON.parse(result);

            for(var i = 0; i < arr.length; i++) {
                var obj = arr[i];   

                var output = document.getElementById("allcomments");  

                output.innerHTML += '<div class="comment-container"><div class="username">'+obj.username+'</div><div class="comment">'+obj.comment+'</div><div class="date">'+obj.commDate+'</div><div class="like">'+obj.sentiment+'</div></div>';

            }

        }
    });

    return false;
}); 

PHP PHP

<?php

require_once('checklog.php');
require_once("db_connect.php");
require_once("functions.php");

session_start();

$username = $_POST['username'];

// Print out existing comment
$query  = "SELECT comments.commDate, comments.ID, comments.username, comments.comment, users.username, comments.sentiment FROM comments LEFT JOIN users ON comments.username = users.username"; 
$result = mysqli_query($db_server, $query);
if (!$result)
    die("Database access failed: " . mysqli_error($db_server));
while ($row = mysqli_fetch_array($result)) {

    $comments[] = $row; 
//CHECK THAT THE COMMENT USERID MATCHES SESSION USER ID
if ($row['username'] == $username['username']){
$comments .=" <a href='delete_post.php?pID=".$row['ID']."'>Delete</a>";
}
        if(!isset($username["liked_" . $row['ID']])){
                $comments .= "<a href='like.php?likeid=" . $row['ID'] ."'>Like</a>";
        }else{  
                $comments .="Liked"; 
        } 
}

mysqli_free_result($result);

require_once("db_close.php");

echo json_encode($comments);

?>

check out this line:看看这一行:

$comments[] = $row; 

You declare $comments as array, then:您将$comments声明为数组,然后:

$comments.=" <a href='delete_post.php?pID=".$row['ID']."'>Delete</a>";

you are appending string to array variable which is absolutely wrong您将字符串附加到数组变量,这是绝对错误的

The result you are returning from server is combination on array and string that result's in invalid format so it will not get converted to the js object.您从服务器返回的结果是数组和字符串的组合,结果格式无效,因此它不会转换为 js object。

暂无
暂无

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

相关问题 错误语法错误:令牌异常 - ERROR Uncaught SyntaxError: Unexpected token <in JSON at position 0 如何修复“Uncaught (in promise) SyntaxError: Unexpected token &lt; in JSON at position 0”错误 - How to fix “Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0” ERROR 如何解决“未捕获的SyntaxError:意外的令牌&lt;”错误 - How to solve “Uncaught SyntaxError: Unexpected token <” error SyntaxError: Unexpected token &lt; in JSON at position 0 如何解决 - SyntaxError: Unexpected token < in JSON at position 0 how to solve it 我该如何解决“未捕获的SyntaxError:JSON位置0上的意外令牌l…?” - How do I solve, “Uncaught SyntaxError: Unexpected token l in JSON at position 0 …?” Redux 应用程序错误:未捕获(承诺中) SyntaxError:意外令牌 &lt; 在 position 0 处的 JSON - Redux app error: Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0 反应错误 - Uncaught SyntaxError: Unexpected token o in JSON at position 1 - react error - Uncaught SyntaxError: Unexpected token o in JSON at position 1 Uncaught SyntaxError: Unexpected token : in JSON at position 362 - Uncaught SyntaxError: Unexpected token : in JSON at position 362 未捕获的语法错误:JSON 中的意外标记 D 位于位置 0 - Uncaught SyntaxError: Unexpected token D in JSON at position 0 未捕获到的SyntaxError:JSON中位置0处的意外令牌 - Uncaught SyntaxError: Unexpected token in JSON at position 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM