Uncought SyntaxError:JSON.parse-一个一直对我有效的AJAX函数在一个新网站上使用,它抛出



这是关于我已经使用了一段时间的脚本。我有一个网站,这个代码仍然可以正常工作,没有任何错误(我甚至从该项目的脚本中复制了代码,因为一旦你做对了,为什么要重新发明轮子?(,但由于某种原因,现在在我的新项目中,我遇到了一些非常严重的错误!我会直接在这里发布一张图片,但我似乎没有足够的声誉。XDhttps://rellawings.com/error.JPG左边图片中的重要细节如下:

Highlighted Above:  var res = JSON.parse(response);
Uncaught SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
success https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/:2045
jQuery 6
<anonymous> https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/:2038
dispatch jQuery
h https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/ line 507 > injectedScript:6
f https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/ line 507 > injectedScript:15
c https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/ line 507 > injectedScript:6
c https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/ line 507 > injectedScript:5
jQuery 8
<anonymous> https://rellawings.com/books/2/Superluminal/1/1.01-What-is-Love?/:2027
1.01-What-is-Love:2045:28

我知道问题出在这个特定的行/函数上:varres=JSON.parse(response(;

但在对我的项目进行编码时突然出现问题,这感觉太离谱了。这很奇怪,因为它总是完美地工作,而且在我的另一个网站上仍然如此。这个AJAX脚本简单易用。。。至少在2022年使用它进行编码时出现错误之前。XD-

使用的jQuery版本与我的其他网站相同。唯一的区别是我没有包含jQuery UI,坦率地说,这应该没有什么区别,对吧?最新的版本被评论掉了,因为我试过切换它们。但这并不能改变我所看到的一切。

这包含在标题html脚本中。

<!---<script src="https://code.jquery.com/jquery-3.6.0.js"></script>-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

这里有一个"有问题"的AJAX脚本,它一直对我有效:

<script>
$("#postbtn").click(function(event){
var b_id = "'.$b_id.'";
var c_id = "'.$c_id.'";
var parent = "0";
var message = $("#commentsbox").html();
console.log("Book ID: '.$b_id.'");
console.log("Chapter ID: '.$c_id.'");
console.log("Is A Child?: " + parent);
console.log("MSG: " + message);
jQuery.ajax({
method: "POST",
url: "post-comment.php",
data: {b_id: b_id, c_id: c_id, parent: parent, message: message},
dataType: "text",
cache: false,
success: function(response){
var res = JSON.parse(response);
if (res.return === "1") {
$("#allcomments").html(res.contentrefresh);
} else {
$("#errorbox").html("There was a problem and your comment could not be posted.").show().delay(8000).fadeOut();
}
}
});
});
</script>

最后,这里是AJAX PHP脚本。我不能100%确定它是否没有错误,但在这种情况下,我无法从AJAX获得任何响应。

有了这个,我正在试验是否可以反馈html格式的内容来刷新页面上的评论。如果是这样的话,我可以实现一个刷新按钮来做同样的事情,只是更新div。我减少了共享代码,因为如果我发布了所有生成评论的内容(与解决此问题无关(,那么请求帮助的时间将超过必要时间。

<?php
include "dbcon.php"; include "functions.php"; session_start();
if($_SESSION['theme'] == "dark"){
$bordercolor = "#DEDEDE";
}
if($_SESSION['theme'] == "light"){
$bordercolor = "#000000";
}
if($_SESSION['theme'] == "sepia"){
$bordercolor = "#5F4B32";
}
$commentsQ = "SELECT * FROM `comments` WHERE `b_id` = '$b_id' && `c_id` = '$c_id' && `parent` = '0' ORDER BY COALESCE(NULLIF(`upvote` - `downvote`,'0'), `updated`) DESC LIMIT 10";
$commentsR = $conn->query($commentsQ);       
$commentsnum = $commentsR->num_rows;
$contentrefresh = '';
while($comment = $commentsR->fetch_assoc()){
$likes = $comment[upvote] - $comment[downvote];
if($likes == 0){
$likes = "";
}else{
$likes = " · '.$likes.' Upvotes";
}
$time = date('m/d/y',$comment['updated']);
$contentrefresh .= '
<div style="width:98%;padding-top:10px;padding-bottom:10px;">
<table style="width:100%;min-height:160px;">
<tr>
<td rowspan="3" valign="top" align="center" width="75px" style="max-width:80px;min-height:160px;padding-right:15px;">
<img src="https://rellawings.com/avatars/'.$comment[uiden].'/'.$comment[avatar].'" class="avatars" title="'.$comment[uname].'" alt="'.$comment[uname].'" />
</td>
<td style="font-size:1.3em;text-align:center;height:50px;" valign="top">
<span style="float:left;font-weight:bold;">'.$comment[uname].'</span><span style="float:right;">'.$time.'</span>
</td>
</tr>
<tr>
<td style="padding-bottom:15px;font-size:1.3em;line-height: 1.8;height:80%;" valign="top">
'.parseemoji($comment[message]).'
</td>
</tr>
<tr>
<td style="font-size:1.3em;cursor:pointer;">
<a id="reply-'.$comment[id].'" title="Reply" alt="Reply">Reply</a>'.$likes.' · <span class="material-icons" style="position:relative;top:4px;">thumb_up</span> · <span class="material-icons" style="position:relative;top:4px;">thumb_down</span>
</td>
</tr>';
/* If There Are Any Replies */
$comments2Q = "SELECT * FROM `comments` WHERE `b_id` = '$b_id' && `c_id` = '$c_id' && `parent` > '0' ORDER BY `updated` DESC LIMIT 10";
$comments2R = $conn->query($comments2Q);
$comment2 = $comments2R->fetch_assoc();     
$commentsnum2 = $comments2R->num_rows;
if($comment['id'] == $comment2['parent']){
while($comment2 = $comments2R->fetch_assoc()){
$likes = $comment2[upvote] - $comment2[downvote];
if($likes == 0){
$likes = "";
}else{
$likes = " · '.$likes.' Upvotes";
}
$time = date('m/d/y',$comment2['updated']);
$body .= '
<div style="width:98%;padding-top:10px;padding-bottom:10px;">
<table style="width:100%;min-height:160px;">
<tr>
<td rowspan="3" valign="top" align="center" width="75px" style="max-width:80px;min-height:160px;padding-right:15px;">
<img src="https://rellawings.com/avatars/'.$comment2[uiden].'/'.$comment2[avatar].'" class="avatars" title="'.$comment2[uname].'" alt="'.$comment2[uname].'" />
</td>
<td style="font-size:1.3em;text-align:center;height:50px;" valign="top">
<span style="float:left;font-weight:bold;">'.$comment2[uname].'</span><span style="float:right;">'.$time.'</span>
</td>
</tr>
<tr>
<td style="padding-bottom:15px;font-size:1.3em;line-height: 1.8;height:80%;" valign="top">
'.parseemoji($comment2[message]).'
</td>
</tr>
<tr>
<td style="font-size:1.3em;cursor:pointer;">
<a id="reply-'.$comment2[id].'" title="Reply" alt="Reply">Reply</a>'.$likes.' · <span class="material-icons" style="position:relative;top:4px;">thumb_up</span> · <span class="material-icons" style="position:relative;top:4px;">thumb_down</span>
</td>
</tr>       
</table>
</div>
';
}
}
$contentrefresh .= '        
</table>
</div>
';
}
$b_id = htmlentities(trim($_POST['b_id']), ENT_NOQUOTES);
$c_id = htmlentities(trim($_POST['c_id']), ENT_NOQUOTES);
$parent = htmlentities(trim($_POST['parent']), ENT_NOQUOTES);
$message = htmlentities(trim($_POST['message']), ENT_NOQUOTES);
if(empty($parent)){
$parent = "0";
}
if(isset($_SESSION['uname'])){
$uiden = $_SESSION['uiden']; $uname = $_SESSION['uname']; $avatar = $_SESSION['avatar'];
$posted = time(); $updated = $posted;
$update = "INSERT INTO `comments` (`b_id`, `c_id`, `parent`, `uiden`, `uname`, `avatar`, `message`, `posted`, `updated`) 
VALUES ('$b_id', '$c_id', '$parent', '$uiden', '$uname', '$avatar', '$message', '$posted', '$updated')";
if ($conn->query($update) === TRUE) {
$response = ['return'=>'1', 'contentrefresh'=>$contentrefresh];
echo json_encode($response);
}else{
$response = ['return'=>'0', 'contentrefresh'=>$contentrefresh];
echo json_encode($response);   
}
}
?>

我真的希望我不必实现一些不必要的复杂纯javascript解决方案来完成这项工作。jQuery是怎么回事,它对语言进行了很好的简化?

这是我第一次修改.htaccess并进行符号链接。所以我的网站使用了伪造的目录,这样它就不会显示丑陋了?mode=etc等等…但这也有一些注意事项。XD我必须直接链接到许多资源。包括AJAX php脚本!哈哈!真好笑!这实际上消除了错误,现在我有了回应!接下来将调试AJAX PHP脚本

谢谢回复!我想我们的谈话让我在这里灵光一现。

jQuery.ajax({
method: "POST",
url: "https://rellawings.com/post-comment.php",
data: {b_id: b_id, c_id: c_id, parent: parent, message: message},
dataType: "text",
cache: false,
success: function(response){
console.log(response);
var res = JSON.parse(response);
if (res.return === "1") {
$("#allcomments").html(res.contentrefresh);
} else {
$("#errorbox").html("There was a problem and your comment could not be posted.").show().delay(8000).fadeOut();
}
}
});

最新更新