I'm working on a web interface for a DYI project. I have limited coding skills as i am on the learning process.
This is part of my code:
var lights_array = [22,23,24,25,26];
function pull_light_status (lights_array){
var devices = {};
devices.veraip = "10.20.174.10";
create_device_array (devices, lights_array);
$.post( "resources/php/individual_json.php", devices)
.done(function (json) {
var vera_obj = JSON.parse(json);
console.log(vera_obj);
});
}
function create_device_array (devices, ligh_arr){
$.each(ligh_arr,function(key,value){
eval("devices.device_"+key+"="+ value);
});
}
pull_light_status(lights_array);
What im trying to do is to generate a post to the invidiviual_json.php so it will process the json request for all the devices and then send me an response i can use in js.
This is the content of individual_json.php
<?php
$veraip = $_POST['veraip'];
foreach ($_POST as $key => $value){
switch ($key){
case "veraip":
break;
default:
echo damejson ($veraip,$value);
break;
}
}
function damejson ($ip,$device){
$url = 'http://' . $ip . '/port_3480/data_request?id=status&DeviceNum=' . $device;
$jsondata_ind = file_get_contents($url);
return $jsondata_ind;
}
?>
Whenever i add the line var vera_obj = JSON.parse(json);
i get an error Uncaught SyntaxError: Unexpected token {
Can someone explain me what am i doing wrong and maybe what can i do to fix it? What i would like is to have a js object in vera_obj i can work with
Thanks in advance
J
EDIT -------------- Changed it to this, i got nothing in console
function pull_light_status (lights_array){
var devices = {};
devices.veraip = "10.20.174.10";
create_device_array (devices, lights_array);
$.getJSON( "resources/php/individual_json.php", {device:devices})
.done(function (vera_obj) {
console.log(vera_obj);
console.log(typeof(vera_obj));
});
}
function create_device_array (devices, ligh_arr){
$.each(ligh_arr,function(key,value){
eval("devices.device_"+key+"="+ value);
});
}