我想为私人消息实现一个小用户自动搜索功能。我找不到下面的代码不起作用的原因。jsajax每次都会在.fail节点中结束,尽管db中有一个以“m”开头的用户。
js公司:
$($userSearchForm).on('keyup', function () {
var $query = $('#userSearch').val();
$
.ajax({
url: '../../../rest/restEndPoints/Privatemessage.php',
type: 'POST',
data: $query
})
.done(function (response) {
console.log(response);
})
.fail(function (error) {
console.log('Username not found', error);
});
});
privatemessage.php rest端点:
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["userSearchForm"])) {
$query = $_POST["userSearch"];
$queryResult = Privatemessage::searchByUsername($conn, $query);
$response = ['success' => $queryResult];
}
类私有消息
static public function searchByUsername(PDO $pdo, $userName) {
$stmt = $pdo->prepare("SELECT u.username FROM Users u WHERE userName LIKE CONCAT('%',:userName,'%')");
$result = $stmt->execute([
'userName' => $userName
]);
$ret = [];
if ($result === true && $stmt->rowCount() > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$loadedUsername = new Privatemessage($pdo);
$loadedUsername->userName = $row['username'];
$ret[] = $loadedUsername;
}
return $ret;
}
return null;
}
privatemessage.php子页上的html:
<form class="form-user-search" id="userSearchForm" action="" method="post">
<div class="form-group">
<label for="userSearch">Find user</label>
<input name="userSearch" id="userSearch" class="form-control" placeholder="Find user" autofocus>
<input name="newPrvMessage" id="newPrvMessage" class="form-control" placeholder="Type in message" autofocus>
<div id="userSearch" style="display:none;"></div>
<button class="btn btn-default btn-block" type="submit" id="userSearchButton">Submit new private message</button>
</div>
</form>
出于某种原因,当我查找正在发送的请求时,响应仅如下所示:
尽管“找不到”类,子页面仍然正确地提取已发送/接收的消息(rest.php文件动态地包含.php类/端点,这取决于您通过navbar输入的子页面)
暂无答案!
目前还没有任何答案,快来回答吧!