【发布时间】:2018-07-01 10:37:14
【问题描述】:
我正在使用http://loopj.com/jquery-tokeninput/,因为我需要将一个选择框直接连接到 DB。
我编写了接受 GET 请求的 PHP 脚本,如他的 GitHub 上所述:
<?php
require_once('sondaggio.php');
# Connect to the database
$s = new Sondaggio();
# Perform the query
$query = sprintf("SELECT id, nome from Regioni WHERE nome LIKE '%s%' LIMIT 5", $s->getRealEscapeString($_GET["q"]));
$arr = array();
$arr = $s->readFromDB($query);
# JSON-encode the response
$json_response = json_encode($arr);
# Return the response
echo $json_response;
?>
类 Sondaggio 构造函数:
function __construct(){
$this->conn = new mysqli($this->host, $this->user, $this->password, $this->database);
// Check connection
if ($this->conn->connect_error) {
exit("Connection failed: " . $this->conn->connect_error);
}
}
函数 readFromDB($query):
public function readFromDB($query){
$arr = array();
$result = $this->conn->query($query);
if ($result->num_rows > 0){
while($row = $result->fetch_object()) {
$arr[] = $row;
}
}
return $arr;
}
一切都经过测试并且运行良好,按照他的文档输出是正确的。但是,当我开始在选择框中输入内容时出现错误:
[Error] TypeError: undefined is not an object (evaluating 'term.replace')
regexp_escape (jquery.tokeninput.js:828)
find_value_and_highlight_term (jquery.tokeninput.js:844:88)
(anonymous function) (jquery.tokeninput.js:899)
each (jquery.min.js:2:11781)
populateDropdown (jquery.tokeninput.js:896)
success (jquery.tokeninput.js:1031)
o (jquery.min.js:2:14739)
fireWith (jquery.min.js:2:15504)
w (jquery.min.js:4:12484)
d (jquery.min.js:4:18320)
所以我遇到了这个错误,我不知道为什么会出现,请帮助我。谢谢
编辑:
var_dump($json_response) (with ...php?q=t) 输出:
string(75) "[{"id":"16","nome":"Toscana\r"},{"id":"17","nome":"Trentino-Alto Adige\r"}]"
【问题讨论】:
-
嗨@big。你能显示
var_dump($json_response);的使用输出吗? -
更新了@DamianDziaduch
标签: javascript php jquery-tokeninput