-3

I have the following code:

function get() {
  var item = '<li class="is-loading">';
  
  $.getJSON(urlJson, function(data){
    $.each(data, function(k,v){
     item = v['title']; // What I need to return
      console.log(url); // See? Here there is the content
    });
  });
  
  console.log('Item: ' + item); // See? Nothing else here is being returned like before
  return item;
}

And the problem is: the return is empty. Inside of each it looks like the value is not goint to title. Any suggestion?

nncl
  • 128
  • 3
  • 9
  • post your json file or some part of json – Mukund Kumar Apr 24 '15 at 15:12
  • @AbdulAhmad it's something like: [ { "title": "Title here" } ] – nncl Apr 24 '15 at 15:16
  • I'm not an expert in this, but did you try `$(data).each(function() {});`? – Abdul Ahmad Apr 24 '15 at 15:19
  • @AbdulAhmad The point is the data is completely irrelevant to the problem at hand. – Etheryte Apr 24 '15 at 15:22
  • @Nit I see, here's the jQuery page on the getJson function, you need to pass the "title" as a parameter to the function http://api.jquery.com/jquery.getjson/ – Abdul Ahmad Apr 24 '15 at 15:23
  • and your getjson function should be `.getJSON` – Abdul Ahmad Apr 24 '15 at 15:26
  • Updated code and still not working: function get() { var url = '
  • '; $.getJSON(urlJson, function(data){ $.each(data, function(k,v){ url = v['title']; // What I need to return console.log(url); // See? Here there is the content }); }); console.log('URL: ' + url); // See? Nothing here is being returned like before return url; }
  • – nncl Apr 24 '15 at 18:36