Welcome to OStack Knowledge Sharing 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...