【问题标题】:AJAX SyntaxError: JSON.parse: unexpected characterAJAX SyntaxError:JSON.parse:意外字符
【发布时间】:2012-08-27 13:50:18
【问题描述】:

我这几天一直在试图找出问题所在。由于我的数据正在发送和检索,但它没有发布到我的 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);
}
?>

【问题讨论】:

  • 语法错误可能在 JSON 响应本身,而不是您的 JavaScript 代码。响应文本是什么样的?
  • 您可能需要考虑使用 Javascript 模板库而不是大量字符串。
  • 哦,是的,这也是 - 不是所有的浏览器都允许你这样换行。 (我认为。)
  • 在哪里可以找到我的回复文本?原谅我,我不会认为自己是专业的 atm。你在 Firbug @pointy 谈论
  • 您在 json 破坏格式之前回显 sql 查询。删除它,它应该可以工作。响应应该只是{"comment_id":"2016","comment_streamitem":"4090","username":"hazy","id":"10","first":"Lucy","middle":"","last":"Botham"}

标签: javascript jquery ajax json


【解决方案1】:

如上所述@thomas。我将 SQL 查询回显到我的函数中。我删除了它,它现在可以正常工作了。

【讨论】:

  • 还有 2 个提示:在 JS 中 data['something']data.something 相同,但后者更具可读性。正如@arxanas 指出的那样,您可以考虑使用 JS 模板系统。我个人用小胡子 (github.com/janl/mustache.js)
  • 谢谢@Thomas,我会采纳你的建议的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-13
  • 1970-01-01
  • 2014-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多