Page 1 of 1

what's wrong with this JSON

Posted: Thu Mar 22, 2007 10:02 pm
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"
    ]
}

Posted: Thu Mar 22, 2007 10:50 pm
by Kieran Huggins
just wondering why you're not using $.getJSON() ?

Posted: Thu Mar 22, 2007 10:57 pm
by Luke
I can't. same reason as last time. this client uses scriptaculous.

Posted: Thu Mar 22, 2007 11:01 pm
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??

Posted: Thu Mar 22, 2007 11:49 pm
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