我正在學習 AJAX。我正在使用香草 JS我想限制通過 API 接收的數據,例如:最多 10 個對象。這是網址:- https://jsonplaceholder.typicode.com/photos問題是當我創建一個 GET 請求時,獲取的數據是大約 5000 個對象。我想使用有限的數據,所以我該怎么做。這是 JavaScript 代碼:const next = document.getElementsByTagName("button"), body = document.querySelector("body");body.addEventListener("DOMContentLoaded",runEvents);function runEvents(){ nextBtn();}function nextBtn(){ //set up the XMLHTTPObject ajax object const xhr = new XMLHttpRequest(); xhr.open("GET", "https://jsonplaceholder.typicode.com/photos", true); xhr.onprogress = function(){ document.getElementsByTagName("img").setAttribute("src", "img/loading.gif"); }; xhr.onload = function(){ if(this.status === 200){ document.getElementsByTagName("p").textContent = "Data Found" //I want to use the data recieved here }else{ document.getElementsByTagName("img").style.display = "none"; document.getElementsByTagName("p").textContent = "Data not found"; } }; }
如何限制從 API 接收的數據?
千巷貓影
2022-10-21 15:24:38