简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM