我正在創建一個網站以在 javascript 中取得進展,但我有一個小問題,我嘗試了各種方法,我的瀏覽器不想加載我的 json 文件。我嘗試了在互聯網上找到的許多代碼,但沒有一個有效(或者我不知道如何使它們工作)。最后,我喜歡這個很容易理解的,但你的一個也不起作用,并且總是返回錯誤消息。function loadJSON(path,success, error){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === XMLHttpRequest.DONE) { if (xhr.status === 1) { if (success) success(JSON.parse(xhr.responseText)); } else { if (error) error(xhr); } } }; xhr.open("GET", path , true); xhr.send();}function test(){ loadJSON('test.json', function(data) { console.log(data); }, function(xhr) { console.error(xhr); });}我運行測試功能,但每次,控制臺都會返回一個錯誤。有人有解決我的問題的想法嗎?
如何在沒有 Jquery 的情況下在本地使用 javascript 加載 json 文件?
收到一只叮咚
2021-09-17 10:22:31