gpt4 book ai didi

javascript - Codeigniter 上的 AutoSuggest (Ajax) - 404 错误

转载 作者:行者123 更新时间:2023-12-02 17:07:41 25 4
gpt4 key购买 nike

我有一个正常运行的 php 应用程序,它完美地使用了 AutoSuggest JS 脚本,现在我正在将相同的应用程序移植到 codeigniter。我不太擅长 CI,这就是我想尝试一下的原因。问题是它不起作用。下面是代码。

JS部分

  var options = {
script:"/getPartnerLogo?",
varname:"input",
json:true,
shownoresults:false,
maxresults:6,
callback: function (obj) { document.getElementById('partner1').value = obj.info;
}
};
var as_json = new bsn.AutoSuggest('pt1', options);

Controller 上的代码

function getPartnerLogo(){ 

$aUsers = array(
"HSBC",
"Spinneys"
);


$aInfo = array(
"HSB",
"SPN"
);

$input = trim($this->input->get('input'));
$len = strlen($input);
$limit = 6;
$aResults = array();
$count = 0;

if ($len)
{
for ($i=0;$i<count($aUsers);$i++)
{

if (strtolower(substr(utf8_decode($aUsers[$i]),0,$len)) == $input)
{
$count++;
$aResults[] = array( "id"=>($i+1) ,"value"=>htmlspecialchars($aUsers[$i]), "info"=>htmlspecialchars($aInfo[$i]) );
}

if ($limit && $count==$limit)
break;
}
}

header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header ("Pragma: no-cache"); // HTTP/1.0

header("Content-Type: application/json");

echo "{\"results\": [";
$arr = array();
for ($i=0;$i<count($aResults);$i++)
{
$arr[] = "{\"id\": \"".$aResults[$i]['id']."\", \"value\": \"".$aResults[$i]['value']."\", \"info\": \"".$aResults[$i]['info']."\"}";
}
echo implode(", ", $arr);
echo "]}";
}

}

现在,当我直接访问 Controller 时,它会正确返回 json。

http://localhost/cd/getPartnerLogo?input=h

{"results": [{"id": "3", "value": "HSBC", "info": "HSB"}]}

但是当我尝试使用 JS 时,它给了我一个 404 错误。当我跟踪网络调用表单检查元素时,响应是 CI 的默认 404 错误页面。

任何人都可以帮我解决这个问题吗?

最佳答案

检查您的请求 URL。
大多数时候,当您在 localhost 上工作时,ajax 请求会由于 URL 中的某些错误而失败。

例如,您的脚本位于localhost/dc下,但您的ajax请求发送到localhost/。您可以在本地主机上配置虚拟主机和设置域,或者在所有 ajax 请求上设置基本 url 必须是绝对 url。
要检查您的请求去了哪里,您可以在开发工具下的 Firefox/Chrome 网络选项卡中检查它们。

关于javascript - Codeigniter 上的 AutoSuggest (Ajax) - 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25102546/

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