Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

javascript - SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data using fetch

I'm using this function

obtenerCursos:async function(){
                    const data = await fetch('cursos.json', {mode: 'no-cors'});
                    const cursos = await data.json();
                    commit('llenarCursos',cursos)
                }

I try to get data from local json file:

[
    {"nombre":"item1", "id":1},
    {"nombre":"item2", "id":2},
    {"nombre":"item3", "id":3}
]

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
type: "opaque"

This means that JavaScript cannot see the content of the response.

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'

Don't do that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...