gpt4 book ai didi

javascript - PHP Ajax错误,输入第一个字符时得到404

转载 作者:行者123 更新时间:2023-12-03 10:57:46 24 4
gpt4 key购买 nike

我在控制台中看到一个错误,它显示:showHint() 未定义,虽然我已经定义了它,但当我键入它时,此实时搜索也不会返回任何内容。它假设应该像this一样工作。

gethint.php 文件:

<?php
// Array with names
$a[] = "Anna";
$a[] = "Brittany";
$a[] = "Cinderella";
$a[] = "Diana";
$a[] = "Eva";
$a[] = "Fiona";
$a[] = "Gunda";
//Further names removed for Post

// get the q parameter from URL
$q = $_REQUEST["q"];

$hint = "";

// lookup all hints from array if $q is different from ""
if ($q !== "") {
$q = strtolower($q);
$len=strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}

// Output "no suggestion" if no hint was found or output correct values
echo $hint === "" ? "no suggestion" : $hint;
?>

index.php

<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","/gethint.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>Start typing a name in the input field below:</b></p>
<form>
First name: <input type="text" onkeyup="showHint(this.value)">
</form>
<p>Suggestions: <span id="txtHint"></span></p>
</body>
</html>

enter image description here

最佳答案

您的错误来自 __DIR__您的 showHint 中未定义该内容功能。删除它或将其更新到您存储自动完成字典的正确路径,您应该就可以了。

回显__DIR__作为您的 gethint.php 的路径文件,执行以下操作:

xmlhttp.open("GET","<?php echo __DIR__; ?>/gethint.php?q=" + str, true);

但是,我假设您不想这样做 __DIR__作为你的道路。最终由您决定如何设置 gethint.php 的路径文件。我们不知道您的应用程序的目录结构或您的文件存储位置。

关于javascript - PHP Ajax错误,输入第一个字符时得到404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28203671/

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