javascript AJAX 语法错误:JSON.parse:意外字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12143349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AJAX SyntaxError: JSON.parse: unexpected character
提问by dave
I've been trying to find out the issue with this for days. As my data is being sent and retrieved, yet it doesn't post into my div.. Why? I did a test and found that I a SyntaxERROR unexpected character.. And can I find it.. Can I heck.
几天来我一直试图找出这个问题。由于我的数据正在发送和检索,但它没有发布到我的 div 中。为什么?我做了一个测试,发现我是一个 SyntaxERROR 意外字符..我能找到它吗..我能不能。
Can someone look through this and tell me of any mistakes please.
有人可以看看这个并告诉我任何错误。
<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>
REPONSE
回应
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 PAGE
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);
}
?>
采纳答案by dave
As @thomas stated above. I was echoing the SQL query out into my function. I removed this and its now working perfectly.
正如@thomas 上面所说。我正在将 SQL 查询回显到我的函数中。我删除了它,现在它可以完美运行。