the error happens because the returned data from ajax request is empty. so you
can't access a variable on empty data.
make sure e holding data before printing variable ..
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
var variable = 'somevalue';
$.get(url, function(e){
console.log( e[0].variable );
});
How can i do something like this, i've tried almost everything but couldn't make it work.
-- The reason i wanna do that is that in the json output there are values like; a1, a2, a3 and i wanna get the value of a1 when its 10pm and get a2 when its 5am ect.
I get an error like : " Cannot read property 'variable' of undefined "
Thanks.
try console.log( e[0][variable] );
var key = 'u1';
var data = [
{
act: null,
id: "1",
a1: "1",
a2: "0",
a3: "0",
u1: "ax-1",
u2: "ax-2",
u3: "ax-3"
}
];
console.log(data[0][key]); // outputs "ax-1" for me
console.log(data[0].key); // undefined
Please or to participate in this conversation.