-1

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

af1223345
  • 45
  • 1
  • 6
  • 1
    Does this answer your question? [Handle response - SyntaxError: Unexpected end of input when using mode: 'no-cors'](https://stackoverflow.com/questions/43317967/handle-response-syntaxerror-unexpected-end-of-input-when-using-mode-no-cors) – Cees Timmerman Nov 20 '20 at 09:29

1 Answers1

0
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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • If I dont's use `{mode: 'no-cors'}` I have another problem because the cors request Its not http – af1223345 Dec 24 '19 at 18:37
  • 1
    @af1223345 — Then run a webserver so it is HTTP. You can't read a file that needs CORS permission to access by telling the browser you don't need CORS and it shouldn't do anything that requires it (which is what `mode: 'no-cors'` means). – Quentin Dec 24 '19 at 18:38
  • ok, i'm running the file directly on the browser without server , this should be the problem – af1223345 Dec 24 '19 at 18:41
  • That is the problem. Use a webserver. – Quentin Dec 24 '19 at 18:41