Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using following code in one of my HTML files

var queryURL = encodeURI(yahooUrl + loc + appId);
alert(queryURL);

$.getJSON(queryURL, function(data){
    alert('inside getJSON')
    alert(data);
    var items = [];
    $.each(data, function(key, value){
        items.push('<li id="' + key + '">' + value + '</li>');
    });
    $('<ul/>', {
        'class': 'my-new-list',
        html: items.join('')
    }).appendTo('body');
});`

where queryURL is one big query which if I load from the browser's address bar I get a file containing a JSON object. But the following code is not working, the whole JSON object is being displayed at the error console of Firefox, with the error 'Invalid Label'. I have added &callback=? at the end of the query string as mentioned in few of the answers here at SO.

Can any one suggest what I am doing wrong ?

Edit: for

queryURL = "http://where.yahooapis.com/geocode?location=107,South%20Market,San%20Jose,San%20Fransico,Leusina,USA,&flags=J&appid=dj0yJmk9SUk0NkdORm9qM2FyJmQ9WVdrOU1tVnFUVzlVTm5NbWNHbzlORFl4TnpZME5UWXkmcz1jb25zdW1lcnNlY3JldCZ4PWE1&callback=?"

I get the following error:

Error: invalid label
Source File: http://where.yahooapis.com/geocode?location=107,South%20Market,San%20Jose,San%20Fransico,Leusina,USA,&flags=J&appid=dj0yJmk9SUk0NkdORm9qM2FyJmQ9WVdrOU1tVnFUVzlVTm5NbWNHbzlORFl4TnpZME5UWXkmcz1jb25zdW1lcnNlY3JldCZ4PWE1&callback=jQuery16404719878257064011_1316606312366&_=1316608283354
Line: 1, Column: 1

Source Code:

{"ResultSet":{"version":"1.0","Error":0,"ErrorMessage":"No error","Locale":"us_US","Quality":87,"Found":1,"Results":[{"quality":39,"latitude":"37.336849","longitude":"-121.847710","offsetlat":"37.338470","offsetlon":"-121.885788","radius":34800,"name":"","line1":"","line2":"San Jose, CA","line3":"","line4":"United States","house":"","street":"","xstreet":"","unittype":"","unit":"","postal":"","neighborhood":"","city":"San Jose","county":"Santa Clara County","state":"California","country":"United States","countrycode":"US","statecode":"CA","countycode":"","uzip":"","hash":"","woeid":2488042,"woetype":7}]}}
share|improve this question
1  
The callback=? is to trigger jsonp mode, which is only useful in you're fetching json data from a domain other than your page. –  Johan Sjöberg Sep 21 '11 at 12:16
    
Yes... as you might guess I am querying Yahoo's API... –  amit Sep 21 '11 at 12:18
1  
Can you post the JSON string, or part of it? It might a problem due to improperly formatted JSON as the string is displayed in the console, and thus returned to the client. –  Jørgen Sep 21 '11 at 12:21
    
or even better the queryURL you send (a generic working example from the browser bar). –  roselan Sep 21 '11 at 12:27
    
please see the edited code,,,, @roselan –  amit Sep 21 '11 at 12:37

1 Answer 1

up vote 6 down vote accepted

This may be caused because jQuery automatically switches to using JSONP (because it's a cross-domain-request) and Yahoo apparently doesn't use JSONP but regular JSON. Have you tried good old $.ajax() with dataType:"JSON"?

Using $.ajax:

        $.ajax({
            url: queryURL,
            dataType: "JSON",
            success: function(data){
              alert('inside getJSON')
              alert(data);
              var items = [];
              $.each(data, function(key, value){
                  items.push('<li id="' + key + '">' + value + '</li>');
              });
              $('<ul/>', {
                  'class': 'my-new-list',
                  html: items.join('')
              }).appendTo('body');
            }
        });

Let me be exceptionally nice here since I'm having a horrible day: Working example

share|improve this answer
    
actually have not tried $.ajax(). Honestly speaking I really dont know how to use it.. –  amit Sep 21 '11 at 12:36
    
I've edited my post with an example. –  joostdevries Sep 21 '11 at 12:40
1  
That will not be your problem. Looking at your code and the JSON returned, you're probably not iterating over the right items. Try iterating over data.ResultSet.Results[0] and if that throws an error, first use data=eval("(" + data + ")"); Also, –  joostdevries Sep 21 '11 at 14:52
2  
See example above ;-) –  joostdevries Sep 21 '11 at 16:10
1  
Holy crap, jsfiddle.net is awesome. –  ladenedge Sep 21 '11 at 16:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.