參考影片:JSON and AJAX Tutorial: With Real Examples
實作網址:https://github.com/nicehorse06/JSON-and-AJAX-practice
參考文章:
var targetUrl = 'https://learnwebcode.github.io/json-example/animals-1.json'//建立 XMLHttpRequest 實例才能發送ajaxvar ourRequest = new XMLHttpRequest();// 設定http方法和目標網址ourRequest.open('GET',targetUrl)// 一但request事件完成後執行該函式,回傳的json為字串,需用JSON.parse轉換成可讀物件ourRequest.onload=()=>{
let ourData = JSON.parse(ourRequest.responseText); console.log(ourData);
}// 一但ourRequest open了即可發送 http 一次,並關閉 open 狀態ourRequest.send()
影片多處理了以下功能,待解釋:
insertAdjacentHTML
ourRequest.status
ourRequest.onerror