【发布时间】:2018-08-15 13:08:28
【问题描述】:
如何在 javascript 中使用 fetch 函数读取本地 JSON 文件? 我有 JSON 文件,其中包含一些转储数据和一个读取服务器上 JSON 文件的函数。 例如:
readJson () {
console.log(this)
let vm = this
// http://localhost:8080
fetch('/Reading/api/file').then((response) => response.json()).then(json => {
vm.users = json
console.log(vm.users)
}).catch(function () {
vm.dataError = true
})
}
那么,在这个 fetch 函数中读取本地 json 文件必须做什么?
【问题讨论】:
-
旁注:您的
fetch呼叫缺少对response.ok的检查;这是我写的一个常见错误a blog post about it。
标签: javascript json