gpt4 book ai didi

javascript - 使用带有ajax和jquery的api调用初始化物化自动完成数据集

转载 作者:行者123 更新时间:2023-12-04 14:54:05 26 4
gpt4 key购买 nike

大家早上好,
我在 PHP 应用程序中使用 materialize.css
我想通过 API 调用来初始化自动完成数据。.autocomplete ()使用 JSON 数组初始化数据 " data ": ,
我用 .ajax ({..., url: '/search.php', ... datas}) 得到的.
但我无法注入(inject) datas.autocomplete () ,
我该怎么做?
这是源代码:

<body>
<form action="">
<div class="row">
<div class="col s12">
<div class="row">
<div class="input-field col s6">
<i class="material-icons prefix">title</i>
<input id="searchOnAPI" name="name" type="text" class="autocomplete" value="{{ item.name }} ">
<label for="searchOnAPI">{{ siteData('admin.titre') }}</label>
<ul id="autocomplete-content" class="autocomplete-content"><li><a></a></li></ul>
</div>
</div>
</div>
</div>
</form>

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script>
$(document).ready(function () {
$('#searchOnAPI').keyup(function () {
$('#autocomplete-content').html('');
let search = $(this).val();

if (search !== "") {
$.ajax({
type: 'GET',
url: '/search.php',
data: 'search=' + encodeURIComponent(search),
success: function (datas) {
if (datas !== "") {
$('#searchOnAPI').autocomplete(datas);
} else {
document.getElementById('autocomplete-content').innerHTML = '<ul class="autocomplete-content dropdown-content"><li><a class="black-text">{{ siteData('site.search.nodata') }}</a></li></ul>';
}
}
});
}
});
});
</script>

我认为问题来自这里:
变量数据...
但我不明白这个错误了。
success: function (datas) {
if (datas !== "") {
$('#searchOnAPI').autocomplete(datas);
}
搜索页面的代码:
并调用 API
<?php

if (isset($_GET['search']) && !empty($_GET['search'])) {
$search = trim(htmlspecialchars($_GET['search']));

$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.themoviedb.org/3/search/multi?api_key=". API_KEY ."&language=fr-FR&query=$search",
CURLOPT_CAINFO => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'lib' . DIRECTORY_SEPARATOR ."cert.tmdb.cer", //api.themoviedb.org
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET"
]);

$response = curl_exec($curl);
$err = curl_error($curl);


$response = json_decode($response, true);
$err = json_decode($err, true);
$datas = [];

if (!is_null($err) || $response === null || !isset($response['results'])) {
$datas[] = ['No Data' => null];
} else {

$response = $response['results'];
for ($i = 0; $i <= 5; $i++) {
if (isset($response[$i]['title'])) {
$datas[] = $response[$i]['title'] ;
} elseif (isset($response[$i]['name'])) {
$datas[] = $response[$i]['name'];
}
}

echo json_encode(array_fill_keys(['data'], array_fill_keys($datas, null)), true);
}

}
这是搜索页面json中的api返回

{
"data": {
"O Amor Acontece": null,
"Quem Quer Namorar Com o Agricultor?": null,
"O'": null,
"O Clone": null,
"Hawaï police d'État": null,
"O Matador": null
}
}
欢迎任何建议!
.EDD

最佳答案

您的 PHP 代码正在返回一个带有“数据”参数的 json 对象,也许您可​​以尝试

   if (datas !== "") {
$('#searchOnAPI').autocomplete(datas.data);
}

关于javascript - 使用带有ajax和jquery的api调用初始化物化自动完成数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68466944/

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