Skip to content

Commit 930934f

Browse files
committed
Autocomplete: Change JSONP demo to use local data source
Fixes #14974
1 parent 586d572 commit 930934f

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

demos/autocomplete/remote-jsonp.html

+10-13
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
.ui-autocomplete-loading {
1111
background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;
1212
}
13-
#city { width: 25em; }
1413
</style>
1514
<script src="../../external/requirejs/require.js"></script>
1615
<script src="../bootstrap.js">
@@ -19,34 +18,31 @@
1918
$( "#log" ).scrollTop( 0 );
2019
}
2120

22-
$( "#city" ).autocomplete({
21+
$( "#birds" ).autocomplete({
2322
source: function( request, response ) {
2423
$.ajax( {
25-
url: "http://gd.geobytes.com/AutoCompleteCity",
24+
url: "search.php",
2625
dataType: "jsonp",
2726
data: {
28-
q: request.term
27+
term: request.term
2928
},
3029
success: function( data ) {
31-
32-
// Handle 'no match' indicated by [ "" ] response
33-
response( data.length === 1 && data[ 0 ].length === 0 ? [] : data );
30+
response( data );
3431
}
3532
} );
3633
},
37-
minLength: 3,
34+
minLength: 2,
3835
select: function( event, ui ) {
39-
log( "Selected: " + ui.item.label );
36+
log( "Selected: " + ui.item.value + " aka " + ui.item.id );
4037
}
4138
} );
4239
</script>
4340
</head>
4441
<body>
4542

4643
<div class="ui-widget">
47-
<label for="city">Your city: </label>
48-
<input id="city" type="text">
49-
Powered by <a href="http://geobytes.com">geobytes.com</a>
44+
<label for="birds">Birds: </label>
45+
<input id="birds">
5046
</div>
5147

5248
<div class="ui-widget" style="margin-top:2em; font-family:Arial">
@@ -55,7 +51,8 @@
5551
</div>
5652

5753
<div class="demo-description">
58-
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are cities, displayed when at least three characters are entered into the field. The datasource is the <a href="http://geobytes.com">geobytes.com webservice</a>. That data is also available in callbacks, as illustrated by the Result area below the input.</p>
54+
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are bird names, displayed when at least two characters are entered into the field.</p>
55+
<p>The datasource is a server-side script which returns JSONP data, specified via a function which uses <code>jQuery.ajax()</code> for the <code>source</code> option.</p>
5956
</div>
6057
</body>
6158
</html>

demos/autocomplete/search.php

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
sleep( 3 );
3+
sleep( 2 );
44
// no term passed - just exit early with no response
55
if (empty($_GET['term'])) exit ;
66
$q = strtolower($_GET["term"]);
@@ -573,7 +573,6 @@
573573
"Heuglin's Gull"=>"Larus heuglini"
574574
);
575575

576-
577576
$result = array();
578577
foreach ($items as $key=>$value) {
579578
if (strpos(strtolower($key), $q) !== false) {
@@ -584,6 +583,12 @@
584583
}
585584

586585
// json_encode is available in PHP 5.2 and above, or you can install a PECL module in earlier versions
587-
echo json_encode($result);
586+
$output = json_encode($result);
587+
588+
if ($_GET["callback"]) {
589+
$output = $_GET["callback"] . "($output);";
590+
}
591+
592+
echo $output;
588593

589-
?>
594+
?>

0 commit comments

Comments
 (0)