【问题标题】:jQueryUI autocomplete returns a 404jQueryUI 自动完成返回 404
【发布时间】:2013-07-31 03:54:34
【问题描述】:

我正在尝试创建一个自动完成输入框,当您在其中单击输入时,会从数据库加载自动完成单词列表。我通过 AJAX 调用从数据库中很好地加载了单词列表,但是当我将它传递给自动完成函数时,它开始返回 404。

我不知道为什么数据在我的警报中显示正常,但无法正确自动完成?

$(document).ready(function() {
    $("#tags").click(function() {
        $.ajax({ 
            url: 'get_players.php', 
            success: function(data){
                alert(data); // This returns the correct string.
                $("#tags").autocomplete({
                    source: data
                });
            }
        });
    });
}); // END Document.ready.

更新:数据以字符串形式返回(我认为)。我想将其用于足球选秀程序,因此 data 是用逗号分隔的球员头衔列表。示例:

'Andy Dalton - QB Bengals', 'Adrian Peterson - RB Vikings', 'Tom Brady - QB Patriots'

这是我的 get_players.php

<?php
//Connect to the Database.
$mysqli = new mysqli("localhost","username","password", "draftboard");

// Check said connection.
if ($mysqli->connect_errno)
{
    print "An error has occured."; 
}

// Get all the players.
$result = $mysqli->query("SELECT * FROM players");

if($result->num_rows > 0) {

    // For each member of the group...
    for($i = 0;  $i < $result->num_rows; $i++)
    {
        $row = $result->fetch_assoc();
        echo "'" .$row['name'] . " - " . $row['position'] . " " . $row['team']."', ";
    }
    $result->close();
}
?>

【问题讨论】:

  • type 是什么data?您能否发布一个对 get_players.php 的 ajax 调用返回的示例?
  • 我用示例值和我的代码更新了问题。谢谢!
  • 您的 source 应该是作为字符串、元素数组或函数传递的 URL。你的source 是这三个中的任何一个吗?我怀疑您只是传递了一个字符串,自动完成功能将其视为一个 URL,但找不到该 URL。因此是 404。
  • 好的,尝试使用:source: data.split(",")
  • 太棒了!像魅力乔一样工作!非常感谢。如果您将评论添加为答案,我会接受。

标签: jquery ajax autocomplete http-status-code-404


【解决方案1】:

autocomplete 需要一个 url、数组或函数。您当前正在传递一个字符串,因此只需使用它来将其转换为数组:

$("#tags").autocomplete({
    source: data.split(",")
});

【讨论】:

    猜你喜欢
    • 2011-02-20
    • 2014-01-30
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 1970-01-01
    相关资源
    最近更新 更多