你好,我想解析從我的數據庫到我的HTML視圖的數據結果。我不太清楚我在做什麼。無法解析JSON編碼的值從PHP到JSON解析在JavaScript中/ jquery
這裏是我的代碼:
function display($dbhandler){
$sql = "SELECT * FROM users";
foreach($dbhandler->query($sql) as $row){
$data = array('id'=>$row['id'],
'email'=> $row['email'],
'name'=> $row['name']
);
echo json_encode($data);
}
}
這會給我的輸出:
{"id":"1","email":"Email 1","name":"Name 1"}{"id":"2","email":"Email 2","name":"Name 2"}'
現在,當我返回並嘗試使用$ .parseJSON(結果)解析它jQuery的。
function display_list(){
\t \t action='select';
\t \t
\t \t $.ajax({
\t \t \t type:"POST",
\t \t \t url:"options.php",
\t \t \t data:{ action : action },
\t \t \t success: function(res){
\t \t \t \t console.log(res)
\t \t \t \t results = $.parseJSON(res);
\t \t \t \t console.log(results);
\t \t \t }
\t \t });
\t }
我得到這個錯誤:
Uncaught SyntaxError: Unexpected token { in JSON at position 66 at Function.parse [as parseJSON]() at Object.success (actions.js:16) at i (jquery-3.1.1.min.js:2) at Object.fireWith [as resolveWith] (jquery-3.1.1.min.js:2) at A (jquery-3.1.1.min.js:4) at XMLHttpRequest. (jquery-3.1.1.min.js:4)
這有什麼錯我試圖傳遞數據?歡迎任何建議,謝謝。
的JSON返回無效,嘗試張貼本網站上的JSON http://jsonlint.com/ ,看到你產生JSON是無效 – ianace