Amit
Amit

Reputation: 13364

How to fetch JSON from a URL using JavaScript?

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}]}}

Upvotes: 2

Views: 866

Answers (1)

joostdevries
joostdevries

Reputation: 930

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

Upvotes: 5

Related Questions