2

The code below doesn't work. Trying to search weather locations. When I search nothing happens.

<input type="text" id="query" /><button>search</button><br />
<div id="results">

</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var url='http://autocomplete.wunderground.com/aq?format=JSON&query=';
var query;
    $('button').click(function(){
        query=$("#query").val();
        $.getJSON(url+query,function(json){
            $.each(json.results,function(i,location){
               $("#results").append('<p>'+location.name+'</p>');
            });
        });
    });
});
</script>

FYI I am very inexperienced at coding (copied script from another website)

3
  • is autocomplete.wunderground.com the domain of the same site from where your page is served? Commented May 9, 2012 at 15:13
  • Where did you get this snippet from? Commented May 9, 2012 at 15:13
  • Parth, no. Bazmega, webhole.net/2009/11/28/how-to-read-json-with-javascript Commented May 9, 2012 at 15:17

2 Answers 2

4

If you want to make cross domain request, you have to that with JSONP, and you should append callback function for JSONP request as mentioned here in wunderground.com, try this.

$(document).ready(function() {
    var url = 'http://autocomplete.wunderground.com/aq?format=JSON&query=';
    var query;
    $('button').click(function() {
        query = $("#query").val();
        $.getJSON(url + query + '&cb=callbackfunc', function(json) {
            $.each(json.results, function(i, location) {
                $("#results").append('<p>' + location.name + '</p>');
            });
        });
    });
});​

UPDATE:

At first you should learn what is JSONP.

cb parameter is for JSONP callback function in wunderground API as you can see here in documentation.

If you still doesn't understand why you need to use jsonp callback function,

open these two links, and you will see what is the differences between them.

without cb paramater

with cb paramater

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry, not good with coding, what should the value of &cb= be?
@ocanal, Thanks a lot for the information above, the links to [without cd, with cb] are the same really, can u tell what is the difference please?
@shireefkhatab yes, there is just only one difference, json is wrapped with mycallbackfunc when you use callback parameter. that means function is called when jsonp request done. if you have a mycallbackfunc in your page, that will be called with a json parameter. jquery handle jsonp request so you don't need to worry about that.
can u give a hint what would cb look like please?
@shireefkhatab, here is a simple jsonp request with pure javascript, jsfiddle.net/j8h6jomb/7
|
0

You can't pull data from remote sites using JavaScript for security reasons (see the same origin policy).

Work arounds involve CORS (limited browser support, not supported by that service), JSON-P (not obviously supported by that service) and using a proxy on your own server.

Comments

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.