javascript - 重新创建 jquery ui 自动完成引发错误
问题描述
我正在尝试 使用我自己的数据库重新创建https://jqueryui.com/autocomplete/#remote-jsonp
test.php 的内容
<?php
$host = "localhost";
$user = "root";
$pass = "*";
$databaseName = "easychartdb";
$connect = mysqli_connect($host,$user,$pass,$databaseName);
$sql = "Select * from `calendar`";
$result = mysqli_query($connect,$sql);
$json_array = array();
$array = mysqli_fetch_row($result);
while($row = mysqli_fetch_row($result))
{
$json_array[] = $row;
}
echo json_encode($json_array);
?>
tester.html 的内容。从链接中改变的是源代码:test.php 和错误函数
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<style>
.ui-autocomplete-loading {
background: white url("images/ui-anim_basic_16x16.gif") right
center no-repeat;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js">
</script>
<script>
$( function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#birds" ).autocomplete({
source: function( request, response ) {
$.ajax( {
url: "test.php",
dataType: "jsonp",
data: {
term: request.term
},
success: function( data ) {
response( data );
}
,
error: function (err) {
console.error("error");
console.log(err);
console.log(err.stack);
}
} );
},
minLength: 2,
select: function( event, ui ) {
log( "Selected: " + ui.item.value + " aka " + ui.item.id );
}
} );
} );
</script>
</head>
<body>
<div class="ui-widget">
<label for="birds">Birds: </label>
<input id="birds">
</div>
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;"
class="ui-widget-content"></div>
</div>
</body>
</html>
控制台日志总是抛出错误
{readyState: 4, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
abort: ƒ ( statusText )
always: ƒ ()
complete: ƒ ()
done: ƒ ()
error: ƒ ()
fail: ƒ ()
getAllResponseHeaders: ƒ ()
getResponseHeader: ƒ ( key )
overrideMimeType: ƒ ( type )
pipe: ƒ ( /* fnDone, fnFail, fnProgress */ )
progress: ƒ ()
promise: ƒ ( obj )
arguments: null
caller: null
length: 1
name: "promise"
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: jquery-1.12.4.js:3431
[[Scopes]]: Scopes[3]
readyState: 4
responseText: "[["2","central medical","ryan","ryan","ryan","2019-02-
06 00:00:00","2019-02-07 00:00:00"]]"
setRequestHeader: ƒ ( name, value )
state: ƒ ()
status: 200
statusCode: ƒ ( map )
arguments: null
caller: null
length: 1
name: "statusCode"
prototype: {constructor: ƒ}
__proto__: ƒ ()
[[FunctionLocation]]: jquery-1.12.4.js:9552
[[Scopes]]: Scopes[3]
0: Closure (ajax) {type: "closure", name: "ajax", object: {…}}
1: Closure {type: "closure", name: "", object: {…}}
2: Global {type: "global", name: "", object: Window}
statusText: "OK"
success: ƒ ()
then: ƒ ( /* fnDone, fnFail, fnProgress */ )
__proto__: Object
谁能帮我解决这里可能出现的问题,谢谢!
解决方案
您正在使用jsonp
但没有返回回调。所以有两种选择。将 dataType 更改为 json 或修复您的返回值:
tester.html:
dataType: 'jsonp',
jsonpCallback: 'callback',
测试.php:
echo $_GET["callback"] . "(" . json_encode($json_array) . ")";
代替
echo json_encode($json_array);
在这里你可以找到一些关于 jsonp 的信息。
推荐阅读
- django - 用于任务计划的 Django 信号
- python - 从 Redis Subcription 在 Jupyter 中显示 Streaming DataFrame
- amazon-web-services - Terraform 策略附加到模块中的角色名称
- r - 如何使用来自一个数据帧的值来通知 R 中另一个数据帧中的求和函数?
- php - ffmpeg的php重命名删除/减少了我的视频文件到20Kb
- python - Multiple Async Context Managers
- apt-get - 处理 vsftpd 时出现 apt-get 安装错误:无法创建目录
- unity3d - ARCore unity - 在两个对象之间放置对象
- datastage - Datastage 并行性与性能改进
- php - 将项目符号添加到预设文本区域输出 - PHP