Javascript 未捕获的语法错误:输入错误意外结束
我必须从我的数据库中编写一个长轮询查询——目前正在使用mysqli。我在javascript代码中成功地调用了数据并返回,但是,现在是关键部分,2分钟后,我收到了这个错误:“uncaughtsyntaxerror:输入意外结束” 我在谷歌上搜索并阅读了几页(我猜超过30页),但我没能解决这个问题。。。 每个人都说我很可能忘了合上括号。。。但我想不是 另一方面,我更喜欢使用Javascript 未捕获的语法错误:输入错误意外结束,javascript,php,json,google-chrome,Javascript,Php,Json,Google Chrome,我必须从我的数据库中编写一个长轮询查询——目前正在使用mysqli。我在javascript代码中成功地调用了数据并返回,但是,现在是关键部分,2分钟后,我收到了这个错误:“uncaughtsyntaxerror:输入意外结束” 我在谷歌上搜索并阅读了几页(我猜超过30页),但我没能解决这个问题。。。 每个人都说我很可能忘了合上括号。。。但我想不是 另一方面,我更喜欢使用json.parse()而不是eval()。。他们两个都不工作 感谢您的关注。 注:我不是英语母语的speacker,很抱歉拼
json.parse()
而不是eval()
。。他们两个都不工作
感谢您的关注。注:我不是英语母语的speacker,很抱歉拼写错误;)
这是我当前的js文件
var old_msg_id=”“;
函数waitForMsg(){
$.ajax({
键入:“获取”,
url:“poll.php?old_msg_id=“+old_msg_id,
async:true,
cache:false,
//数据类型:“json”,
成功:功能(数据响应){
//var jsonAnswer=eval(“+dataRespond+”);
var jsonAnswer=JSON.parse(dataRespond);
如果(jsonAnswer.msg!=“”){
警报(“新的味精添加到基础中!”);
log(jsonAnswer.msg);
};
old_msg_id=jsonAnswer.old_msg_id;
setTimeout('waitForMsg()',1000);
},
错误:函数(XMLHttpRequest、textStatus、errorshown){
警报(“Deu merda!:错误:“+textStatus+”(“+ErrorSprown+”));
setTimeout('waitForMsg()',15000);
}
});
}
$(文档).ready(函数(){
waitForMsg();
});
这是我的poll.php
错误报告(0);
$conn=mysql_connect(“本地主机”、“根目录”、“根目录”);
mysql_select_db('padova',$conn)或die('not select database');
$result=mysql_query(“根据id DESC LIMIT 1从测试订单中选择id”);
如果($result==FALSE){
die(mysql_error());
}
$old_msg_id=$_GET['old_msg_id'];
$result=mysql_query(“按id DESC LIMIT 1从测试顺序中选择id、文本”);
while($row=mysql\u fetch\u数组($result))
{
$last_msg_id=$row['id'];
$msg=$row['text'];
}
while($last_msg_)欢迎来到Stack Overflow!我删掉了你的长篇介绍,因为这里不需要,而且被认为是“噪音”。请看一下和指南。你的第一个问题不是灾难性的,但阅读这些问题通常很重要。不过,这个问题与Chrome扩展有什么关系?我的错误标签:)对不起。@Xan,谢谢你的建议,男士:)你看了服务器返回的内容了吗,我猜它不是正确的JSON。
var old_msg_id = "<?php echo $old_msg_id; ?>";
function waitForMsg() {
$.ajax({
type: "GET",
url: "poll.php?old_msg_id=" + old_msg_id,
async: true,
cache: false,
//dataType : 'json',
success: function (dataRespond) {
//var jsonAnswer = eval("(" + dataRespond + ")");
var jsonAnswer = JSON.parse(dataRespond);
if (jsonAnswer.msg !== "") {
alert("New msg added to base!");
console.log(jsonAnswer.msg);
};
old_msg_id = jsonAnswer.old_msg_id;
setTimeout('waitForMsg()', 1000);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Deu merda!: Error: " + textStatus + " (" + errorThrown + ")");
setTimeout('waitForMsg()', 15000);
}
});
}
$(document).ready(function () {
waitForMsg();
});
error_reporting(0);
$conn = mysql_connect("localhost", "root", "");
mysql_select_db('padova', $conn) or die('Could not select database.');
$result = mysql_query("SELECT id FROM test ORDER BY id DESC LIMIT 1");
if($result === FALSE) {
die(mysql_error());
}
$old_msg_id = $_GET['old_msg_id'];
$result = mysql_query("SELECT id, text FROM test ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
$last_msg_id = $row['id'];
$msg = $row['text'];
}
while($last_msg_id <= $old_msg_id)
{
usleep(1000);
clearstatcache();
$result = mysql_query("SELECT id, text FROM test ORDER BY id DESC LIMIT 1");
while($row = mysql_fetch_array($result))
{
$last_msg_id = $row['id'];
$msg = $row['text'];
}
}
$response = array();
$response['msg'] = $msg;
$response['old_msg_id'] = $last_msg_id;
$response = array_map('htmlentities',$response);
echo json_encode($response);