jQuery自动完成库返回一个错误的未找到的网址和没有数据显示在文本框
我必须在我的应用程序中使用jquery-ui做自动完成文本框。我有这样的Ajax脚本从数据库中获取数据:jQuery自动完成库返回一个错误的未找到的网址和没有数据显示在文本框
<script>
$(function() {
$("#searchTxt").on('keyup', function(){
searchTxt = $("#searchTxt").val();
$.ajax({
url: '../php/autoComplete.php',
data: {searchTxt: searchTxt},
type: 'GET',
dataType: 'JSON',
success:function(resp)
{
$.each(resp, function(key, result)
{
var availableTags = result['patient_name_en'];
$("#searchTxt").autocomplete({
source: availableTags
});
});
},
error:function(resp)
{
console.log(resp)
}
})
});
});
</script>
这里是autoComplete.php脚本:
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('connection.php');
$cid = $_SESSION['clinic_id'];
$searchTxt = '%'.$_GET['searchTxt'].'%';
$res = array();
$getPatients = "SELECT * FROM patient WHERE clinic_id = :cid and patient_name_en LIKE :searchTxt ORDER BY patient_id DESC";
$execGetPatients = $conn->prepare($getPatients);
$execGetPatients->bindValue(':cid', $cid);
$execGetPatients->bindValue(':searchTxt', $searchTxt);
$execGetPatients->execute();
$getPatientsResult = $execGetPatients->fetchAll();
$i = 0;
foreach($getPatientsResult as $result)
{
$res[$i] = $result;
$i++;
}
echo json_encode($res);
?>
的问题是,我可以看到在的网络标签返回的数据开发者工具,但与其他文件一起返回的The requested URL /ncd/... was not found on this server.
错误下面是对情况的图像,我可以看到一个数组,没有错误返回:
这里是其他红色文件:
在控制台:
我试图从get
类型更改为post
但仍然是相同的问题。
您没有正确的结构。您的ajax
呼叫应该是autocomplete
的功能。你有它反转,它看起来像它被分配给你的自动完成元素的意外参数。
这里看到这个例子以供参考:https://jqueryui.com/autocomplete/#remote
下面这个例子,如果我们假设你的AJAX端点返回正确的结构化数据,你的代码应该是这个样子:
$("#searchTxt").autocomplete({
// I'd recommend using an absolute url.
source: "/ncd/php/autoComplete.php"
});
如果你打开您的控制台和检查在documented example响应,JSON数据的结构,像这样:
[
{"id":"Nycticorax nycticorax","label":"Black-crowned Night Heron","value":"Black-crowned Night Heron"},
{"id":"Corvus cornix","label":"Hooded Crow","value":"Hooded Crow"},
{"id":"Corvus corone","label":"Carrion Crow","value":"Carrion Crow"},
etc...
]
所以你可能想要更新你的端点结构响应的方式。
我得到这个错误,当我改变它为你:'注意:未定义的索引:C:\ wamp64 \ www \ ncd \ php \ autoComplete.php中的searchTxt在第7行上 – droidnation
更多帮助将不胜感激 – droidnation
查看[source option]的文档(http://api.jqueryui的.com /自动填充/#选项源)。您需要在前端和后端同步查询参数。 – klenwell
根据网址“ncd/pages/Ahmad ..”正在被解雇。找出这个url模式写在你的代码中的地方。可能与搜索“NCD /页” – Ashish451
可能相关的问:https://*.com/questions/6431459/jquery-autocomplete-trigger-change-event –
@ Ashish451它是生成一个网址与我搜索的名称在文本框 – droidnation