【发布时间】:2018-04-03 03:44:27
【问题描述】:
我正在为我的 laravel 项目中的表创建搜索功能。我正在使用 ajax 实时搜索方法,但现在在插入搜索栏中时遇到了一些错误。控制台中显示的错误是 POST 404(未找到)。 我的帖子在下面。我不确定为什么它停止工作了。
对不起,我的英语不好。
这是我的脚本
$(document).ready(function(){
load_data();
function load_data (query)
{
$.ajax({
url:"fetch.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#table').html(data);
}
});
}
$('#search').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
fetch.php
<?php
if(isset($_POST["query"]))
{
$connect = mysqli_connect("localhost", "root", "", "testing");
$request = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM domain
WHERE Domain LIKE '%".$request."%'
";
$result = mysqli_query($connect, $query);
$html = '';
$data = array();
$html .= '
<table class="table table-bordered table-striped">
<tr>
<th>Domain</th>
<th>Registrar</th>
<th>Login ID</th>
<th>Password</th>
</tr>
';
while ($row = mysqli_fetch_array($result))
{
$data[] = $row["Domain"];
$data[] = $row["Registrar"];
$data[] = $row["Login_ID"];
$data[] = $row["Password"];
$html .= '
<tr>
<td>'.$row["Domain"].'</td>
<td>'.$row["Registrar"].'</td>
<td>'.$row["Login_ID"].'</td>
<td>'.$row["Password"].'</td>
</tr>
';
}
$html .= '</table>';
if (isset($_POST["typehead_search"]))
{
echo $html;
}
else
{
$data = array_unique($data);
echo json_encode($data);
}
}
?>
【问题讨论】:
-
你能分享一下确切的错误吗?你可以缩短你的
fetch.php,所以它现在只打印一些简单的东西,因为问题似乎是 JS 击中 PHP 文件而不是 PHP 文件本身。 -
这段代码真的是用 laravel 写的吗?
-
看起来你所有的东西都是用纯 php 写的。