gpt4 book ai didi

javascript - Devbridge JQuery 自动完成不适用于 serviceUrl

转载 作者:行者123 更新时间:2023-11-29 19:13:57 27 4
gpt4 key购买 nike

我正在尝试实现 devBridge 自动完成功能,但它不适用于 ajax。当我使用不带 php 的手动 json 对象但我需要从数据库中获取数据时,它会起作用。PHP 代码:

 <?php
require_once "medoo.php";
$database = new medoo([
'database_type' => 'mysql',
'database_name' => 'elektriksikayet',
'server' => 'localhost',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'port' => '3307'
]);
$return_arr = [];
$keyword = $_POST['keyword'];
$companies = $database->select('companies','company_name');
foreach ($companies as $comp){
$row_array['label'] = $comp;
array_push($return_arr, $row_array);
}
echo json_encode($return_arr);
?>

HTML 代码:

<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="js/jquery.js"></script>
<script src="js/jquery-autocomplete.js"></script>
<script src="js/companyAutoComplete.js"></script>
</head>
<body>
<input type="text" id="autocomplete"/>
</body>
</html>

JS 代码

$(document).ready(function () {
$("#autocomplete").autocomplete({
serviceUrl:'AutoComplete.php', //tell the script where to send requests
//callback just to show it's working
onSelect: function(value){ alert('You selected: ' + value); }
});
});

最佳答案

您的回复应该是有效的 JSON 格式且格式如下:

   {
suggestions: [
{ "value": "United Arab Emirates", "data": "AE" },
{ "value": "United Kingdom", "data": "UK" },
{ "value": "United States", "data": "US" }
]
}

您返回的内容似乎没有这种结构。

关于javascript - Devbridge JQuery 自动完成不适用于 serviceUrl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36726039/

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