gpt4 book ai didi

javascript - 为什么我的评论没有被发布?

转载 作者:行者123 更新时间:2023-12-02 14:14:12 24 4
gpt4 key购买 nike

HTML 部分:

        <div class="page-data">
Page data is in here.
</div>

<div class="comment-wrapper">
<h3 class="comment-title">Feedback:</h3>

<div class="comment-insert">

<h3 class="who-says">Says: Leon Burning</h3>
<div class="comment-insert-container">
<textarea id="comment-post-text" class="comment-insert-text"></textarea>
</div>

<div class="comment-post-btn-wrapper" id="comment-post-btn">
Post
</div>
</div>

<div class="comments-list">

<ul class="comments-holder-ul">
<?php $comments = array( "a", "b", "c" , "d"); ?>
<?php require_once INCLUDES . 'comment_box.php';?>
</ul>
</div>
</div>

</div>

<input type="hidden" id="userId" value="1" />
<input type="hidden" id="userName" value="Leon Burning" />

</body>

<---JavaScript (comment_insert.js)---->

$(document).ready( function(){
$('#comment-post-btn').click(function() {
comments_post_btn_click();
});
});
function comments_post_btn_click() {
var _comment = $('#comment-post-text').val();
var _userId = $( '#userId' ).val();
var _userName = $( '#userName' ).val();

if( _comment.length > 0 && _userId !=null ) {
$('#comment-post-text').css('border','0px solid red');

$.ajax({
type: "POST",
url: "ajax/comments_insert.php",
data: {
task : "comment_insert",
userId : "userid",
comment : _comment
},
success : function(data)
{
comment_insert( jQuery.parseJSON(data));
console.log("ResponseText " + data); },
});

console.log(_comment + " / Username: " + _userName + " / User-ID " + _userId );
}
else {
$('#comment-post-text').css('border','1px solid red');
console.log("Area was empty.");
}

$('#comment-post-text').val("");

}

function comment_insert() {
var t = "";
t += '<li class="comments-holder" id"_1">';
t += '<div class="user-img">';
t += '<img src="user_img.png" class="user-img-pic" />';
t += '</div>';
t += '<div class="comment-body">';
t += '<h3 class="username-field">Insert User</h3>';
t += '<div class="comment-text">New Comment</div>';
t += '<div class="comment-buttons-holder">';
t += '<ul>';
t += '<li class="delete-btn">X</li>';
t += ' </ul>';
t += '</div>';
t += '</div>';
t += '</li>';

$( '.comments-holder-ul' ).prepend( t );
}

<---comments_insert.php--->

<?php

print_r( $_REQUEST );

?>

Firefox 错误消息:

“语法错误:JSON.parse:JSON 数据第 1 行第 1 列出现意外字符”

如果有人能为我解决这个问题,我将非常感激! :)

最佳答案

您正在尝试将 ajax 调用的响应解析为 JSON:

成功:函数(数据)
{
comment_insert( jQuery.parseJSON(data));
console.log("ResponseText"+ 数据); },
});

这不会起作用,因为您只是打印出 $_REQUEST 数组:

print_r( $_REQUEST );

您需要先将数组转换为 JSON:

echo json_encode( $_REQUEST );

关于javascript - 为什么我的评论没有被发布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39191415/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com