使用該無法解析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