gpt4 book ai didi

javascript - PHP/AJAX 解析错误

转载 作者:行者123 更新时间:2023-11-28 05:05:31 25 4
gpt4 key购买 nike

我正在尝试使用我的 PHP 呼喊框并使用 AJAX 进行查询,这样页面就不必重新加载来发布呼喊。任何帮助将不胜感激!

我陷入困境 - 我不断收到解析错误:

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>

还有 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,
]));

?>

最佳答案

您的解析错误来自此行:

$("#aData").text(数据);

您无法将 Json 编码数据发送到 DOM 中的 id。您想查看数据的值(value),请尝试以下操作:

警报(JSON.stringify(数据));

另一件事,在你的 php 中,避免退出,忘记 if 和 else。

正确的方法是尝试,捕获。你尝试一下,如果发生错误,捕获异常。

将全局变量放在函数外部,并将 awswer 放入内部。

刚刚之后:

回显 json_encode($aswer);$连接=空;//始终关闭连接。

希望对你有帮助。

关于javascript - PHP/AJAX 解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41786860/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com