gpt4 book ai didi

javascript - Select2.js 错误 : Cannot read property 'length' of undefined

转载 作者:可可西里 更新时间:2023-11-01 13:53:57 25 4
gpt4 key购买 nike

我正在使用 Select2 jquery 插件,但无法使用 json 获取结果。在浏览器中查看 json 响应时,它看起来没问题。例如:

[{
"id" : "50",
"family" : "Portulacaceae "
}, {
"id" : "76",
"family" : "Styracaceae "
}, {
"id" : "137",
"family" : "Dipsacaceae"
}
]

在这种情况下使用 ajax 调用的 URL 是:http://localhost/webpage/json_family.php?term=acac&_=1417999511783 但我无法在 select2 输入中得到结果,控制台说:

Uncaught TypeError: Cannot read property 'length' of undefined

代码如下:
html

<input type="hidden" id="select2_family" name="term" style="width:30%" />

js

$("#select2_family").select2({
minimumInputLength: 3,
ajax: {
url: "json_family.php",
dataType: 'json',
data: function (term) {
return {
term: term,
};
},
results: function (data) {
return { results: data.results };
}

}
});

php

$myArray = array();
if ($result = $mysqli->query("SELECT id,family FROM family WHERE family LIKE '%$term%'")) {
$tempArray = array();
while($row = $result->fetch_object()) {
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
}

代码有没有错误?

最佳答案

好的,我的测试服务器上有你的示例,请执行以下操作

将您的查询更改为此,为了可读性更改了一些名称,但应该具有相同的功能,重要的部分是在查询中添加“AS TEXT”

$query = $mysqli->query("SELECT id, family AS text FROM family WHERE family LIKE '%$term%'"));
while ($row = mysql_fetch_assoc($query)) {
$return[] = $row;
}

echo json_encode($return);

其次,看起来您正在尝试从名为“results”的 json 响应中调用一个属性

如果是这种情况,您的 json 应该如下所示,请注意,由于上述更改,family 现在是文本:

{
"results":
[
{
"id": "50",
"text": "Portulacaceae "
},
{
"id": "76",
"text": "Styracaceae "
},
{
"id": "137",
"text": "Dipsacaceae"
}
]
}

但是您的 php 不会创建属性结果,因此请更改您的结果函数以删除 .results 属性调用

   results: function (data) {
return { results: data };
}

我使用的最终代码(请注意,我没有转义/清理 $_GET[term] 或将其绑定(bind)到查询,建议您这样做)如果您仍有问题,我可以向您发送指向我的网站示例的链接

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.css">
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.js"></script>
<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.js"></script>
</head>
<script>
$(document).ready(function () {

$("#select2_family").select2({
minimumInputLength: 3,
ajax: {
url: "select2.php",
dataType: 'json',
data: function (term) {
return {
term: term,
};
},
results: function (data) {
return { results: data };
}
}
});

});
</script>

<input type="hidden" id="select2_family" name="term" style="width:30%" />

</html>

php

<?

/*** connection strings ***/

// get the database singleton instance
$yog = MySqlDatabase::getInstance();

// connect
try {
$yog->connect($host, $user, $password, $db_name);
}
catch (Exception $e) {
die($e->getMessage());
}

$term = $_GET['term'];

if (!$term){
$sub = $yog->query("SELECT id, family AS text FROM family");
} else {
$sub = $yog->query("SELECT id, family AS text FROM family where family like '%$term%'");
}

while ($row = mysql_fetch_assoc($sub)) {
$return[] = $row;
}

echo json_encode($return);

?>

关于javascript - Select2.js 错误 : Cannot read property 'length' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27349882/

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