what's wrong with this JSON

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

what's wrong with this JSON

Post by Luke »

Is there something wrong with my ajax? For some reason, as soon as I try to eval() the response, javascript no longer works. There are no errors in my error console at all. What could be causing this??

request

Code: Select all

    new Ajax.Request('ajax.php',
    {
        asynchronous:true,
        method: 'post',
        postBody: 'action=load_block&block_select=' + show_name,
        onSuccess: function(transport)
        {
    	    alert('TEST1');  // this gets alerted
            var response = transport.responseText || null;
            alert(response); // shows the json below...
            var data = eval("(" + response + ")");
            alert('TEST2'); // this does not
        },
        onFailure: function()
        {
        	alert('Something went wrong...');
        }
    });
response:

Code: Select all

{
    "0":[
        "name":"Apacalypto",
        "id":"8",
        "duration":"149"
    ],
    "1":[
        "name":"Apacalypto",
        "id":"9",
        "duration":"149"
    ],
    "2":[
        "name":"Alpha Dogg",
        "id":"10",
        "duration":"148"
    ],
    "3":[
        "name":"Alpha Dogg",
        "id":"11",
        "duration":"148"
    ],
    "4":[
        "name":"Alpha Dogg",
        "id":"12",
        "duration":"148"
    ]
}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

just wondering why you're not using $.getJSON() ?
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I can't. same reason as last time. this client uses scriptaculous.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

I changed the []s to {}s, and it works now. JSON can't have associative arrays?

EDIT: Well it evals now, but I can't access anything within the data var. WTF? Why doesn't this work??
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

"associative arrays" in javascript are just objects.

Code: Select all

{
    "0":{
        "name":"Apacalypto",
        "id":"8",
        "duration":"149"
    },
..... 
see if that works for you...

http://www.json.org is a nice compact reference
Post Reply