![](/img/trans.png)
[英]Using Fetch: SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
[英]SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data using fetch
我正在使用这个功能
obtenerCursos:async function(){
const data = await fetch('cursos.json', {mode: 'no-cors'});
const cursos = await data.json();
commit('llenarCursos',cursos)
}
我尝试从本地 json 文件中获取数据:
[
{"nombre":"item1", "id":1},
{"nombre":"item2", "id":2},
{"nombre":"item3", "id":3}
]
但我没有得到 json 数据我得到了一个对象
Response
body: null
bodyUsed: false
headers: Headers
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""
我不确定我做错了什么
type: "opaque"
这意味着 JavaScript无法看到响应的内容。
由于JS看不到内容,所以它能看到的内容长度为零。 因此,在找到任何使其成为有效 JSON 的内容之前,它会到达内容的末尾。
它是不透明的,因为你说:
mode: 'no-cors'
不要那样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.