0

I'm trying to load a JSON file by link and then align data (like title, date etc) to variables so I can use them. Right now, I don't care about variables. I just want to alert() them but something seems like I'm doing it wrong, alert returns nothing!

I use JSfidle to run the code. The code is this:

var JSON_unparsed = $.getJSON('http://www.14deftera.gr/feeds/posts/default?orderby=published&alt=json') ;

var JSON = JSON.parse(JSON_unparsed) ;

alert(JSON.feed.entry[0].title.$t) ;

The URL I want to parse is: http://www.14deftera.gr/feeds/posts/default?orderby=published&alt=json

and here you can see the JSON how is structured if that can help you:

JSON structure

Draken
  • 3,134
  • 13
  • 34
  • 54
  • 5
    First of all, `$.getJSON` is ***asynchronous***. Secondly, the URL can't be loaded, as it's not JSONP or have CORS headers, and therefore is in violation of the same-origin policy. – adeneo Apr 04 '16 at 16:47
  • 4
    Don't name the variable `JSON`. – noahnu Apr 04 '16 at 16:48
  • 2
    [How do I return the response from an asynchronous call](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) and [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – adeneo Apr 04 '16 at 16:50
  • I used this and still nothing : var raad = $.getJSON('http://www.14deftera.gr/feeds/posts/default?orderby=published&alt=json') ; var rad = JSON.parse(raad) ; alert(rad.feed.title.$t) ; – Radwan Ahmet Apr 04 '16 at 16:50
  • Both comments are relevant. If you overwrite JSON with the parsed JSON you'll never be able to use JSON.xxx in a meaningful way. But the first comment explains why it doesn't work the first time. – Dave Newton Apr 04 '16 at 16:50
  • can someone fix the code ? i am not a professional and that problem just stops me from finishing a project. thanks – Radwan Ahmet Apr 04 '16 at 16:55
  • 1
    It can't be fixed, the data you're trying to get ***can not be gotten*** with a browser – adeneo Apr 04 '16 at 16:56

2 Answers2

1

In addition to adeneo's reply, in your code, JSON_unparsed variable is holding something called (differed or promise object), this object might be holding the data inside it,but you are using the wrong way to pull it out. in order for you to get it out, you need to call (.done()) function, see the below:

var JSON_unparsed = $.getJSON('http://www.14deftera.gr/feeds/posts/default?orderby=published&alt=json').done(function(json){
console.log(json);
console.log(json.feed.entry[0].title.$t);
});

aside from that, if you got an error with something like this:

XMLHttpRequest cannot load http://www.14deftera.gr/feeds/posts/default?orderby=published&alt=json&_=1459788714707. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://stackoverflow.com' is therefore not allowed access.

it means that you are not allowed to call this API/URL from your current domain.

One more thing, if you are using getJSON method, there is no need to parse the returned data, jquery will parse it for you

Ahmed Ayoub
  • 694
  • 1
  • 7
  • 12
  • thanks! But i still have a problem, I dont know what i am doing wrong, but still doesnt working. http://i.imgur.com/83T2IF8.png – Radwan Ahmet Apr 04 '16 at 17:15
  • @RadwanAhmet, $ is not defined error means that either you didn't include jQuery library, or you didn't wrap your code within the ready function: $(function(){}); it would be better if you used jsfiddle to post your code so we can help you better. Mehdi's reply below is fine, I visited his codepen and it seems that he forgot to attach the jQuery library, once I did so, it worked just fine. – Ahmed Ayoub Apr 04 '16 at 17:22
  • now i puted the jquery i found this code nice too. but how can i attach the output to a variable ? – Radwan Ahmet Apr 04 '16 at 17:36
1

You can use JSONP for this:

Update, for better understanding how to work with returned JSON.

var id, title;

$.ajax({
    url: 'http://www.14deftera.gr/feeds/posts/default?orderby=published&alt=json', 
    jsonp: "callback",    
    dataType: "jsonp"  
}).done(function(r){
    // r is returned JSON
    for(var i in r)    
        // for ex ID is this
        id = r[i].id.$t;
        // and title
        title = r[i].title.$t;
        // and so on, check the json, I mean check the browser console by hitting F12, below code will print the whole JSON 
        console.log(r[i]);
});

Codepen link: http://codepen.io/m-dehghani/pen/grXrrp?editors=0010

Mehdi Dehghani
  • 10,970
  • 6
  • 59
  • 64