Solved

jQuery, AJAX and JSON (oh my!)

Posted on 2014-07-27
6
655 Views
Last Modified: 2014-08-02
Hi experts,

I am trying to understand and adapt the example code provided in the autocomplete documentation

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<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;
}
#city { width: 25em; }
</style>
<script>
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://gd.geobytes.com/AutoCompleteCity",
dataType: "jsonp",
data: {
q: request.term
},
[b]success: function( data ) {
response( data );[/b]
}
});
},
minLength: 3,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="city">Your city: </label>
<input id="city">
Powered by <a href="http://geonames.org">geonames.org</a>
</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>

Open in new window


I'm trying to convert it to use another API which appears to work successfully, but won't display the results.

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Remote JSONP datasource</title>
<link rel="stylesheet" href="jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>

<style>
.ui-autocomplete-loading {
background: white url("images/ui-anim_basic_16x16.gif") right center no-repeat;
}
#city { width: 25em; }
</style>
<script>
$(function() {
function log( message ) {
$( "<div>" ).text( message ).prependTo( "#log" );
$( "#log" ).scrollTop( 0 );
}
$( "#city" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "http://api.addressify.com.au/address/suburbAutoComplete",
dataType: "json",
data: {
term: request.term,
api_key: "{removed}"
},
success: function( data ) {
response( data );
}
});
},
minLength: 3,
select: function( event, ui ) {
log( ui.item ?
"Selected: " + ui.item.label :
"Nothing selected, input was " + this.value);
},
open: function() {
$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
},
close: function() {
$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
}
});
});
</script>
</head>
<body>
<div class="ui-widget">
<label for="city">Your city: </label>
<input id="city">
Powered by <a href="http://geonames.org">geonames.org</a>
</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>

Open in new window



I just don't see what I'm doing wrong.
The API call works because I have the ability of checking how many API calls left and that changes with each keystroke.

Any advice?
0
Comment
Question by:Tim Palmer
  • 5
6 Comments
 
LVL 3

Author Comment

by:Tim Palmer
ID: 40223455
I should also add, the documentation about the API I am using is found here
http://www.addressify.com.au/documentation.html
0
 
LVL 3

Author Comment

by:Tim Palmer
ID: 40223467
Also, an example of the response from addressify.com.au is

["STRATFORD","STRATHCEDAR","STRATHEDEN","STRATHFIELD","STRATHFIELD SOUTH"]
0
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 40225308
The request is probably from another domain which is prohibited.
You should use jsonp instead of json as the dataType.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 3

Author Comment

by:Tim Palmer
ID: 40225379
I tried jsonp too with no luck.

Using just Ajax and not jquery I can populate a select box using http_get_contents but I'd like to make it "pretty"

This code works for me, but not using AJAX/jQuery, this is a manual AJAX call that most of my AJAX calls use (jQuery is still foreign to me so I prefer going the ways I know)

$term = $_GET['term'];
$temp = array("api_key"=>"{removed}","term"=>$term, "state"=>"nsw","max_results"=>5);
$query = http_build_query($temp);
$list = file_get_contents("http://api.addressify.com.au/address/suburbAutoComplete?$query");
echo $list;

Open in new window

0
 
LVL 3

Accepted Solution

by:
Tim Palmer earned 0 total points
ID: 40225625
OK! I got it!

I had to modify my autocomplete, but it looks like "term" is passed automatically to my getSuburb.php (posted above) and thats done the trick!

$(function() {

$( "#city" ).autocomplete({
source: "getSuburb.php",
minLength: 3

})
});

Open in new window

0
 
LVL 3

Author Closing Comment

by:Tim Palmer
ID: 40235876
Simplified the code myself and it worked.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery delay animation 3 37
Converting dates in JavaScript 2 51
Firing an ASP.NET event on selection of a JQuery datepicker 4 30
multiple selects 23 48
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question