0
我正在嘗試將某些產品返回到類設置爲預先輸入的字段。我目前有一個php頁面,我通過查詢數據庫並通過JSON返回結果。當我附和響應我得到如下:來自PHP文件的Typeahead.js和JSON數據
[
{
"id": "97",
"Product": "Amazon Fire TV",
"Description": "Amazon - Fire TV Streaming Device - Black"
},
{
"id": "98",
"Product": "Amazon Aurum Ultra Series - High Speed HDMI Cable (100 Ft) With Ethernet - Supports 3D",
"Description": "High Speed HDMI Cable With Ethernet - Supports 3D & Audio Return Channel"
},
{
"id": "99",
"Product": "Amazon Eco Dot",
"Description": "Amazon Eco Dot"
},
{
"id": "100",
"Product": "Amazon Fire TV",
"Description": "Amazon Fire TV"
},
{
"id": "101",
"Product": "Amazon Linear 5445 ChannelPlus Four-Channel Video Modulator",
"Description": "Four-Channel Video Modulator"
},
{
"id": "102",
"Product": "Amazon Pyle PCM20A 40 Watts Power Amplifier with 25 and 70 Volt Output",
"Description": "70 Volt Power Amplifier with 400 Watts"
}
]
,我遇到的問題是,我的預輸入的輸入框沒有顯示這些數據。以下是我在HTML頁面的頭部中爲該部分提供的代碼。
<script>
$(document).ready(function() {
var Product = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: '/includes/estimate-search.php?query=%QUERY',
wildcard: '%QUERY'
}
});
$('.typeahead').typeahead(null, {
name: 'Product',
source: Product
});
});
</script>
我的PHP代碼解析的結果如下:
<?php
require_once('../database/connection.php');
$query = $_GET['query']; // add % for LIKE query later
// do query
$stmt = $pdo->prepare("SELECT `id`, `Product`, `Description` FROM `products` WHERE `Product` LIKE '%".$query."%'");
$stmt->bindParam(':query', $query, PDO::PARAM_STR);
$stmt->execute();
// populate results
$results = array();
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$results[] = array('id' => $row['id'], 'Product' => $row['Product'], 'Description' => $row['Description']);
}
// and return to typeahead
header('Content-type: application/json');
echo json_encode($results, JSON_PRETTY_PRINT);
return json_encode($results);
?>
我缺少什麼,導致該結果不是由預輸入API來顯示?在此先感謝您的幫助!
對於首發,錯誤處理和日誌條目。 – symcbean
您正在回顯並返回json編碼的結果。它是一個錯誤還是一個功能?但它似乎不是你失敗的原因... – steven
我只是有它回聲,所以我可以確保我得到了預期的結果。 –