[英]How do I retrieve a JSON encoded array printed inside a <div> from a JavaScript file?
[英]retrieve an array from a php encoded json and append it to a div body
我已經找了幾個小時,卻找不到答案。
我正在發送這個數組
$dailyHours = ['8:00','8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30'
,'15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30'];
使用以下代碼嵌入json:
$list = array('hours' => $dailyHours);
$c = json_encode($list);
echo $c;
我想在html div上顯示它。 為此,我使用以下代碼:
success: function(msg,string,jqXHR){
$("#result").html(msg.hours);
}
這不起作用,我得到的錯誤是:
未捕獲的TypeError:無法在“節點”上執行“ appendChild”:參數1的類型不是“節點”。
我猜這是行不通的,因為我要么不能那樣編碼,要么不能那樣附加。 有什么辦法嗎? 謝謝
編輯:console.log(msg.hours)的結果
{ “小時”: “8:00”, “8:30”, “9:00”, “9:30”, “10:00”, “10:30”, “11:00”,“11 :30" , “12:00”, “12:30”, “13:00”, “13:30”, “14:00”, “14:30”, “15:00”,“15:30 ”, “16:00”, “16:30”, “17:00”, “17:30”, “18:00”, “18:30”, “19:00”, “19:30”] }
msg.hours
是一個數組,jQuery的.html()
方法接受字符串作為參數。
您應該首先通過toString()方法將數組轉換為字符串。
嘗試這個:
$("#result").html(msg.hours.toString());
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.