0

I'm trying to use geopages.org api to populate a city input field onblur of my zip code input field. I don't know what the problem is with my script.

<script>

$("#zip").blur(function() {
var city = $("#city");
var zip = $("#zip");

$.getJSON("http://www.geonames.org/postalCodeLookupJSON?postalcode=" + zip.val + "&country=US&username=myusername?", function(json){
 data = json.postalcodes[0].placeName;
 city.val(text);                    
});
}
});

</script>

I'm pretty sure it's a syntax error but my jQuery is not that good so I don't know where my issue is.

3
  • can you take all of your code to jsfiddle ? Commented Feb 28, 2012 at 2:46
  • make sure the API is returning JSONP other wise if it is straight json you'll need a proxy or service like YQL to get the data first Commented Feb 28, 2012 at 2:48
  • I'm new to this JSON and JSONP stuff. This is the api i'm using. Commented Feb 28, 2012 at 2:51

1 Answer 1

1

You have one more } than needed, try this:

$("#zip").blur(function() {
var city = $("#city");
var zip = $("#zip");

$.getJSON("http://www.geonames.org/postalCodeLookupJSON?postalcode=" + zip.val + "&country=US&username=myusername?", function(json){
 data = json.postalcodes[0].placeName;
 city.val(text);                    
});
});
Sign up to request clarification or add additional context in comments.

12 Comments

Here's a working jsfiddle: jsfiddle.net/WmtCr/1. The only thing I can see as the reason for this not working is if you did not enable your free web service. Uncomment the alert to see the message if an error occurred. Of course, replace the username with a valid username... Note I used the example from their side, i.e. it's a call with fixed parameters - you should change back to what you had, changed as I put it in the answer to read back from your zip.
Does it matter that you're using a text area in jsfiddle but that I'm using a text box in my application?
You need to use $('#zip').val() to get the value of the zip - doing without .val() will get you the jQuery wrapper around the DOM object only. Updated: jsfiddle.net/WmtCr/6, just replace your username and click in and out of the text box - you should get San Diego.
There can be a lot of things that can go wrong - did you include jQuery, do you do everything in onload (i.e. $(function() { ... }), etc. My advice - read through the code to see you don't have any syntax errors, break it into small pieces, try to mimic what's on jsFiddle as that's working. Hope something will work out for you!
Here you are: jsfiddle.net/WmtCr/17. This wraps it in $(function() { ... }. Try copying this code locally and running - should work. To read more about this: docs.jquery.com/Tutorials:Introducing_$(document).ready()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.