簡體   English   中英

AJAX SyntaxError:JSON.parse:意外字符

[英]AJAX SyntaxError: JSON.parse: unexpected character

幾天來我一直試圖找出問題所在。 在發送和檢索我的數據時,它沒有發布到我的div中。為什么? 我進行了測試,發現我出現了SyntaxERROR意外字符。並且可以找到它。

有人可以瀏覽一下,並告訴我任何錯誤。

<script>
$(document).ready(function(){
    $("form#mycommentform").submit(function() {

        var streamidcontent = $(this).children("#streamidcontent").val();
        var contents = $(this).children("#contents").val();
        $.ajax({
            type: "POST",
            url: "comment_add.php",
            cache: false,
            dataType: "json",
            data: { streamidcontent: streamidcontent, contents: contents }, 
            success: function(data){  
                $("#containerid").html('<div class="stream_comment_holder" id="comment_holder_'+data['comment_streamitem']+'">\
                <div id="comment_list_'+data['comment_streamitem']+'">\
                <div id="tgy"></div><div class="stream_comment" id="comment_'+data['comment_id']+'" style="margin-top:0px;">\
                <table width=100%><tr><td valign=top width=30px></a><td valign=top align=left>\
                <a href="/profile.php?username='+data['username']+'">'+data['first']+'</a>\
                <div class="commentholder">'+data['first']+'</div><br/>\
                <div id="commentactivitycontainer"></div></div></table></div></div>\
                <div class="form"><form id="mycommentform" method="POST" class="form_statusinput">\
                <input type="hidden"  name="streamidcontent" id="streamidcontent" value="'+data['comment_streamitem']+'">\
                <input type="text" name="contents" id="contents" placeholder="Say something" autocomplete="off">\
                <input type="submit" id="button" value="Feed"></form></div>\
                <div class="stream_comment_holder" style="display:;"><div class="like_name"><b>\
                <a href="profile.php?username='+data['username']+'">You Like This</a>\
                </b></div></div>');
                  alert("SUCCESS!!!");
 },
 error: function (xhr, ajaxOptions, thrownError) {
    alert(xhr.statusText);
    alert(xhr.status);
    alert(thrownError);
 }
 });
        return false
    });
 });
 </script>

回應

INSERT INTO streamdata_comments(comment_poster, comment_streamitem, comment_datetime, comment_content) VALUES (10,4090,UTC_TIMESTAMP(),'fff'){"comment_id":"2016","comment_streamitem":"4090","username":"hazy","id":"10","first":"Lucy","middle":"","last":"Botham"}

PHP頁面

<?php
session_start();
require"include/rawfeeds_load.php";

if(isset($_POST['streamidcontent'])&isset($_POST['contents'])){
rawfeeds_user_core::add_comment($_POST['streamidcontent'],$_POST['contents']);


$json = array();
$check = "SELECT comment_id, comment_datetime, comment_streamitem, comment_poster FROM streamdata_comments WHERE comment_poster='".$_SESSION['id']."' AND comment_streamitem='".$_POST['streamidcontent']."' ORDER BY comment_datetime DESC";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_array($check1);
$json['comment_id'] = $resultArr['comment_id'];
$json['comment_streamitem'] = $resultArr['comment_streamitem'];
mysqli_free_result($check1);

$check = "SELECT * FROM users WHERE id=".$_SESSION['id']."";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_array($check1);
$json['username'] = $resultArr['username'];
$json['id'] = $resultArr['id'];
$json['first'] = $resultArr['first'];
$json['middle'] = $resultArr['middle'];
$json['last'] = $resultArr['last'];
mysqli_free_result($check1);

echo json_encode($json);
}
?>

正如上面的@thomas。 我正在將SQL查詢回顯到我的函數中。 我刪除了它,現在可以正常工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM