SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Json

  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face Json

    Hi im new to JSON so im not surprised that its been a slight uphill battle but i believe im 1 line of code from being able to put JSON behind me today and i just cant quite crack it

    Response: http://www.radiopayback.com/getimage...2010%20Mix%203 Stupidly long (its a google search request for an image),

    i only want 1 element:
    Code JavaScript:
    "unescapedUrl":"http://nfo.sceper.eu/nfo/000-va--mos_presents_addicted_to_bass_winter-3cd-2010-oma.png"

    but i cant seem to get the syntax correct within my script:
    Code JavaScript:
    var obj = new Object();
    var obj = $.getJSON("/getimage.php?path=" + artist + " - " + title);
    console.log(obj);
    var cover = new Object();
    var cover = obj.responseData.results[0].unescapedUrl
    console.log(cover);

    console confirms the obj variable is being passed the JSON information including the "responseText".
    error i am getting is "Uncaught TypeError: Cannot read property 'results' of undefined"


    any help would be greatly appreciated

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    AJAX calls are asynchronous; i.e. at the time your code fires the data probably hasn't returned yet. Which is why $.getJSON has a callback function, you should use that instead:

    Code javascript:
    var obj = $.getJSON("/getimage.php?path=" + artist + " - " + title, function(data) {
      var cover = data.results[0].unescapedUrl
      console.log(cover);
    });

    (or make the call synchronous, but 999 out of 1000 times that's a bad idea)

    Also see jQuery.getJSON() – jQuery API
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou your ofcourse correct i cant believe i didnt see that,

    i am now however getting an error that makes 0% sense to me what so ever:

    Uncaught TypeError: Cannot read property '0' of undefined
    (anonymous function)RPJS.html:95
    f.extend._Deferred.e.resolveWithjquery-latest.min.js:16
    wjquery-latest.min.js:18
    f.support.ajax.f.ajaxTransport.send.djquery-latest.min.js:18
    RPJS.html:94


    let me expand on the entire function although i dont think its relivent

    Code JavaScript:
    //Load2 Function
    	  function Load2() {
            xmlhttp2.open("GET","http://www.radiopayback.com/previous.php",false);
            xmlhttp2.onreadystatechange = function() {
              if(xmlhttp2.readyState == 4) {
                var xmlDoc2=xmlhttp2.responseXML;
                var x=xmlDoc2.getElementsByTagName("entry");
                for (i=0;i<x.length;i++) { 
    			  var artist = x[i].getElementsByTagName("artist")[0].childNodes[0].nodeValue;
    			  var title = x[i].getElementsByTagName("track")[0].childNodes[0].nodeValue;
    			  var obj = $.getJSON("/getimage.php?path=" + artist + " - " + title, function(data) {
    				console.log(obj);
    				var cover = data.results[0].unescapedUrl;
    				console.log(cover);
    			  });
    			  document.getElementById(i).innerHTML="<a href='http://grooveshark.com/#/search?q=" + artist +" - "+ title + "' target='_blank'>" + artist +" - "+ title + "</a>";
                }
              }
            }
            xmlhttp2.send(null);
          }

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    That's because I left out the responseData ... oops

    Anyway, why are you using xmlhttp in the outer loop and jQuery in the inner loop? This should also work:

    Code javascript:
    function Load2() {
       $.get('http://www.radiopayback.com/previous.php', function(data) {
          var x=data.getElementsByTagName("entry");
          for (i=0;i<x.length;i++) { 
            var artist = $('artist', x).text();
            var title = $('track', x).text();
            $.getJSON("/getimage.php?path=" + artist + " - " + title, function(data2) {
               var cover = data2.responseData.results[0].unescapedUrl;
               console.log(cover);
            });
            $('#'+i).html("<a href='http://grooveshark.com/#/search?q=" + artist +" - "+ title + "' target='_blank'>" + artist +" - "+ title + "</a>");
         }
       }
    }

    (not tested)
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response,

    i actually just gave up on JSON an hour or so ago and decided to do all the heavy lifting in PHP with a regex and then add another child element to the XML.

    /me should of thought of this in the first place


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •