2

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}]}}
4
  • 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. Commented Sep 21, 2011 at 12:16
  • Yes... as you might guess I am querying Yahoo's API... Commented Sep 21, 2011 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. Commented Sep 21, 2011 at 12:21
  • or even better the queryURL you send (a generic working example from the browser bar). Commented Sep 21, 2011 at 12:27

1 Answer 1

5

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

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

4 Comments

actually have not tried $.ajax(). Honestly speaking I really dont know how to use it..
thanks... I have tried the above code.. seems working but the list is prepared character by character... and is not much readable. I think yahoo is sending a big long "String" instead of JSON. . .
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,
When I tried data.ResultSet.Results[0].... Got an error in error console that ResultSet is not defined.... The problem got solved when I replace line data:"format=json" with your suggested dataType:"JSON" ... thanks for your help....

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.