Javascript PHP/AJAX解析错误
我尝试使用AJAX获取我的PHP shoutbox和查询,这样页面就不必重新加载才能发布shoutbox。任何帮助都将不胜感激 我被卡住了-我一直收到一个解析错误:Javascript PHP/AJAX解析错误,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我尝试使用AJAX获取我的PHP shoutbox和查询,这样页面就不必重新加载才能发布shoutbox。任何帮助都将不胜感激 我被卡住了-我一直收到一个解析错误: 18:47:27.216 Details: parsererror Error:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 1 index.php:100:4 我有shoutBox.php,它包含HTML和AJA
18:47:27.216 Details: parsererror
Error:SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data 1 index.php:100:4
我有shoutBox.php,它包含HTML和AJAX:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<script type="text/javascript">
function sendChatMessage(msg)
{
$.ajax({
type: "POST",
url: "login/postData.php",
data: {
thisPost: msg
},
dataType: "json",
success: function(data)
{
if (data.error === false)
{
$("#aData").text(data);
console.log("Post submitted.");
ajax.reload();
}
else
{
console.log("Post was not submitted.");
}
},
error: function(xhr, desc, err)
{
console.log(xhr);
console.log("Details: " + desc + "\nError:" + err);
}
});
}
</script>
<div class="shoutbox">
<h1 class="panel-heading"><strong>Federal</strong> Communications Array</h1>
<hr>
<ul class="shoutbox-content"></ul>
<div class="panel">
<form method="post">
<p>
<table border="0" align="center" style="height: auto;" width="90%">
<tbody>
<tr>
<td><a></a></td>
<td>
<div class="input-group">
<span class="input-group-addon">Say:</span>
<input class="form-control" id="aData" name="post" maxlength='255' placeholder="Enter a message..."></input>
<div class="input-group-btn">
<button class="btn btn-success" onClick="sendChatMessage(document.getElementById('aData').value); return false;">Send</button>
</div>
</div>
</td>
<td></td>
</tr>
</tbody>
</table>
<p>
</form>
</div>
<div class="panel">
<table class="table table-striped">
<thead>
<tr>
<th width="100px">Timestamp</th>
<th width="120px">Username</th>
<th width="980px">Message</th>
<th></th>
</tr>
</thead>
</table>
</div>
</div>
函数sendChatMessage(msg)
{
$.ajax({
类型:“POST”,
url:“login/postData.php”,
数据:{
发帖人:msg
},
数据类型:“json”,
成功:功能(数据)
{
如果(data.error==false)
{
$(“#aData”)。文本(数据);
console.log(“Post submitted”);
ajax.reload();
}
其他的
{
日志(“未提交帖子”);
}
},
错误:函数(xhr、desc、err)
{
console.log(xhr);
console.log(“详细信息:+desc+”\n错误:+err);
}
});
}
联邦通信阵列
说:
发送
时间戳
用户名
消息
以及postData.php,它(应该)执行查询
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
echo('Script begin.');
require "dbconf.php"; // db details
$connect = mysql_connect($host,$username,$password);
mysql_select_db($db_name,$connect);
$post = '';
if(empty($_POST['aData']))
{
exit(json_encode([
'error' => 'You must enter a message!',
]));
} else {
$post = trim($_POST['aData']);
$playerUsername = $_SESSION['username'];
$idQuery = "SELECT `id` FROM `members` WHERE `username`='$playerUsername'";
$playeridQuery = mysql_query($idQuery);
$playerID = mysql_result($playeridQuery, 0);
mysql_query("INSERT INTO `shoutboxPosts` SET `id`='$playerID', `username`='$playerUsername', `post`='$post'");
}
exit(json_encode([
'error' => false,
]));
?>
删除一行
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
//echo('Script begin.'); Remove this line. It's violating JSON format
require "dbconf.php"; // db details
$connect = mysql_connect($host,$username,$password);
mysql_select_db($db_name,$connect);
$post = '';
if(empty($_POST['aData']))
{
exit(json_encode([
'error' => 'You must enter a message!',
]));
} else {
$post = trim($_POST['aData']);
$playerUsername = $_SESSION['username'];
$idQuery = "SELECT `id` FROM `members` WHERE `username`='$playerUsername'";
$playeridQuery = mysql_query($idQuery);
$playerID = mysql_result($playeridQuery, 0);
mysql_query("INSERT INTO `shoutboxPosts` SET `id`='$playerID', `username`='$playerUsername', `post`='$post'");
}
exit(json_encode([
'error' => false,
]));
?>
删除一行
<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL | E_STRICT);
//echo('Script begin.'); Remove this line. It's violating JSON format
require "dbconf.php"; // db details
$connect = mysql_connect($host,$username,$password);
mysql_select_db($db_name,$connect);
$post = '';
if(empty($_POST['aData']))
{
exit(json_encode([
'error' => 'You must enter a message!',
]));
} else {
$post = trim($_POST['aData']);
$playerUsername = $_SESSION['username'];
$idQuery = "SELECT `id` FROM `members` WHERE `username`='$playerUsername'";
$playeridQuery = mysql_query($idQuery);
$playerID = mysql_result($playeridQuery, 0);
mysql_query("INSERT INTO `shoutboxPosts` SET `id`='$playerID', `username`='$playerUsername', `post`='$post'");
}
exit(json_encode([
'error' => false,
]));
?>
您的分析错误来自以下行:
$(“#aData”)。文本(数据)
无法将Json编码的数据发送到DOM中的id。要查看数据的值,请尝试以下操作:
警报(JSON.stringify(数据))
另外,在php中,避免退出,忘记if和else
正确的方法是尝试,抓住。如果发生错误,您可以尝试捕获异常
将全局变量放在函数外,将awswer放在函数内
刚刚:
echo json_编码($aswer);
$connect=null;//始终关闭您的连接
我希望它能对您有所帮助。您的分析错误来自以下行:
$(“#aData”)。文本(数据)
无法将Json编码的数据发送到DOM中的id。要查看数据的值,请尝试以下操作:
警报(JSON.stringify(数据))
另外,在php中,避免退出,忘记if和else
正确的方法是尝试,抓住。如果发生错误,您可以尝试捕获异常
将全局变量放在函数外,将awswer放在函数内
刚刚:
echo json_编码($aswer);
$connect=null;//始终关闭您的连接
我希望它能帮助您。echo(“脚本开始”)代码>您还可以接受SQL注入。更新您的驱动程序并使用参数化查询。@chris85我计划实现PDO-只是找到正确的ajax方法。谢谢。您的AJAX失败是因为您告诉您正在返回JSON,但根据@HPierce注释,您正在生成无效的JSON。您在成功插入时也发送了错误JSON。@chris85我确实删除了“echo”,但仍然收到相同的分析错误。不太确定它的来源。echo('scriptbegin')代码>您还可以接受SQL注入。更新您的驱动程序并使用参数化查询。@chris85我计划实现PDO-只是找到正确的ajax方法。谢谢。您的AJAX失败是因为您告诉您正在返回JSON,但根据@HPierce注释,您正在生成无效的JSON。您在成功插入时也发送了错误JSON。@chris85我确实删除了“echo”,但仍然收到相同的分析错误。不太确定它的起始位置。我删除了该行,但在第1行第1列仍收到相同的解析错误。我删除了该行,但在第1行第1列仍收到相同的解析错误。