iterate over json returned by success function

first off the error i get is "Uncaught TypeError: Cannot read property 'length' of undefined"
            i am using the following method to make a get request to a url
            now just for testing lets say vurl = "test.cfc?method=xyz&lnk=1&lnk2=abc"
            on success when i alert data i get: {"LNK2":"abc","_":1.332177382469E12,"LNK":1.0}
            so what am i doing wrong here?            
            $.ajax( {
                        type: "get",                        
                        url: vurl,                  
                        datatype: "json",
                        success: function(data) {
                              i've tried a variety of things here to loop through the json but get the error      

thanks ...
LVL 25
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gurvinder Pal SinghCommented:
try this way

for ( key in data )
   alert( date[ key ] );
dgrafxAuthor Commented:
the code you posted loops over each character as in:



Gurvinder Pal SinghCommented:
first, tell me the output of


and then]

alert( typeof data );
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

dgrafxAuthor Commented:
data = {"LNK2":"abc","_":1.332177382469E12,"LNK":1.0}
typeof = string

so how do i convert to json? i imagine thats the issue ...
the url does have returnformat=json
Gurvinder Pal SinghCommented:
got it!

before you run that for loop on this JSON, call this

data = eval (data);
dgrafxAuthor Commented:
i get Uncaught SyntaxError: Unexpected token :
Gurvinder Pal SinghCommented:
dgrafxAuthor Commented:
i ended up using:
$.each(data,function(ii,jj){ ..........

this works as expected (i believe)

is there code that says if the returned var is not an object to turn it into one?
thanks for your time
Greetings dgrafx,

First of all, eval is evil and should not be used in a real javascript application; there are many tricks that can allow you to have the same functionality without the security issues eval provides.

Second, alert is a bad way to debug an application. Use console.log instead, assuming you are using a modern browser it should be enough.

For instance:

success: function(data) {

Open in new window

If you already know the object and are $.each 'ing over it, then just do whatever you want for each element. For instance,

var o = [{name: 'jjperezaguinaga'}, {name: 'dgrafx'}, {name: 'john'}]

$.each(o, function(index, value) { // Use meaningful variable names ;)
    // $(this) can be used too...
    /* Your code here */
} );

Open in new window

For your question about the object, here's a nice trick to help you out

var name = || {}

Open in new window

This means that if we have a name in our object data, assign it, other wise, create an empty object. This a common javascript pattern that you get to see around many libraries :)

Last but not least, EE has a Code tag to help review code. Use it! :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.