在我的网站上,我有一个搜索框。我试图关闭自动填充建议,并创建一个从数据库中填充建议的Div。
所以我的HTML是这样的:
<input type="text" id="searchbox" onkeyup="searchq();" autocomplete="off">
<input type="image" src="media/icons/search.png" id="search_button">
<div id="search_suggestions"></div>
然后我有一个像这样的JS函数,它接受输入的内容并使用单独的php文件查询数据库:
function searchq(){
var search_text = $("#searchbox").val();
console.log(search_text);
$.post("get_search_suggestions.php", {search_text: search_text}, function(output){
$("#search_suggestions").html(output);
});
}
然后我的PHP文件如下:
$product_search_result = "";
if(isset($_POST['search_text'])){
$term = $_POST['search_text'];
$new_term = '%'.$term.'%';
$params = [$new_term];
$sql = "SELECT * FROM products WHERE name LIKE ?";
$stmt = DB::run($sql,$params);
$resultCount = $stmt->rowCount();
if($resultCount > 0){
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["pro_id"];
$product = $row["".$lang.""];
$params1 = [$id];
$sql1 = "SELECT * FROM products WHERE id=?";
$stmt1 = DB::run($sql1,$params1);
while ($row = $stmt1->fetch(PDO::FETCH_ASSOC)){
$url = $row["url"];
$product_name = $row["product_name"];
}
$product_search_result .= "<a href='product/$url'>";
$product_search_result .= translate($product_name);
$product_search_result .= "</a>";
}
}
}
echo $product_search_result;
这在Windows和Android上对我来说很好用,但是一个Mac用户在控制台中看到以下内容:
Cross-Origin Read Blocking (CORB) blocked cross-origin response https://www.lifting365.com/beta/get_search_suggestions.php with MIME type text/html. See https://www.chromestatus.com/feature/5629709824032768 for more details.
任何有关解决此问题的建议或想法将不胜感激。我不确定为什么这些文件被视为跨域的,尽管这些文件位于主目录的同一目录或子目录中。