Jump to content

$.each() giving error in chrome console


dzhax

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.

Link to comment
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.

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...