使用该无法解析JSON
var res = eval('(' + response + ')');
的response
包含[{"total_sections":"3"}]
此值不能解析JSON。但我不能采取值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);
}
});
}
管理布局:
public function manageLayouts() {
$id = $_POST['id'];
$data = $this->managelayoutmodel->getSections($id);
echo json_encode($data);
}
你应该避免使用函数eval(),在这里阅读原因:http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea和这里http ://javascript.crockford.com/code.html – 2012-04-03 05:56:27
'alert(typeof response)'的结果是什么?'在调用eval之前? – scessor 2012-04-03 06:05:37
猜测,你刚刚在通话中错过了[[0]]?它应该可以通过'res [0] .total_sections' – Christoph 2012-04-03 06:09:54