SyntaxError:JSON.parse:使用fetch在JSON数据的第1行第1列中的数据意外结束 [英] SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data using fetch
本文介绍了SyntaxError:JSON.parse:使用fetch在JSON数据的第1行第1列中的数据意外结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用此功能
obtenerCursos:async function(){
const data = await fetch('cursos.json', {mode: 'no-cors'});
const cursos = await data.json();
commit('llenarCursos',cursos)
}
我尝试从本地json文件获取数据:
I try to get data from local json file:
[
{"nombre":"item1", "id":1},
{"nombre":"item2", "id":2},
{"nombre":"item3", "id":3}
]
但是我没有得到一个对象的json数据
But I'm not getting json data I get a object
Response
body: null
bodyUsed: false
headers: Headers
ok: false
redirected: false
status: 0
statusText: ""
type: "opaque"
url: ""
我不确定自己在做什么错
I'm not sure about what I'm doing wrong
推荐答案
type: "opaque"
这意味着JavaScript 看不到响应的内容.
This means that JavaScript cannot see the content of the response.
由于JS无法看到内容,因此它可以看到的内容长度为零.因此,在找到任何可以使之成为有效JSON的内容之前,它会到达内容的结尾.
Since JS can't see the content, the content it can see has zero length. So it gets to the end of the content before finding anything that would make it valid JSON.
它是不透明的,因为您说过:
It is opaque because you said:
mode: 'no-cors'
不要那样做.
这篇关于SyntaxError:JSON.parse:使用fetch在JSON数据的第1行第1列中的数据意外结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文