Can not parse json using this
var res = eval('(' + response + ')');
The response contain [{"total_sections":"3"}] this value. But i couldn’t take the value total_sections.
function changeSection() {
id = document.getElementById('layoutDropdown').value;
$.ajax({ type: "POST",
url:'/manage',
data: {id : id },
success: function(response){
alert(response);
var res = eval(response);
alert(res[0].total_sections);
}
});
}
manage Layouts:
public function manageLayouts() {
$id = $_POST['id'];
$data = $this->managelayoutmodel->getSections($id);
echo json_encode($data);
}
If
responseis astringyou can parse the json withvar res = eval(response);(but it isn’t nice),var res = JSON.parse(response);or if you use jQueryvar res = $.parseJSON(response). Otherwise if it is already anobjectyou don’t need to parse it, only setvar res = response;.Now you can get the total sections by calling
res[0].total_sections, e.g. you can test it withalert(res[0].total_sections);.