Jump to content
Sign in to follow this  
dzhax

$.each() giving error in chrome console

Recommended Posts

Hello All!

 

It's been a while.

 

Trying to parse a json string and running into some issues.

<script type="text/javascript">    $().ready(function(){	var $_GET = {};	document.location.search.replace(/??(?[^=]+)=([^&]*)&?)/g, function () {    	    function decode(s) {                return decodeURIComponent(s.split("+").join(" "));    	    }    	    $_GET[decode(arguments[1])] = decode(arguments[2]);        });	//console.log($_GET["t"]);	$.get("assets/php/all_channels.php?t="+$_GET["t"],function(data){/*    START TROUBLE CODE*/	    $.each(data.channels, function(i,object){    	        $.each(object, function(property, value) {        	    console.log(property + "=" + value);    		});	    });/*    END TROUBLE CODE*/        });			    });</script>

In my chrome console I get the following:

Uncaught TypeError: Cannot read property 'length' of undefined 

I have tracked the issue down to my .each code because commenting it out eliminates the error. I have added comments to surround the troublesome code.

 

Any assistance with this issue is much appreciated.

Share this post


Link to post
Share on other sites

what's data? are you logging your response to make sure it is what you expect it be? sounds like it's not an array.

Share this post


Link to post
Share on other sites

oh i think i got it.

 

I didn't realize it was looking for an array (not sure why i didn't think of that)

 

I added

var json = jQuery.parseJSON(data);

the changed data.channels to json.channels.

 

now it seems to work.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...