M
M
Mothersprogrammer2020-08-20 15:21:46
Vue.js
Mothersprogrammer, 2020-08-20 15:21:46

How to parse json in vue cli?

It is necessary to transfer data from the local json file to the application, I tried to do it like this:

fetch('@/todo.JSON')
            .then(result => result.json())
            .then(data => {
                  this.todos = data.contents;
            })


but the error is displayed in the console: localhost /: 1 Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0,
I still didn’t understand what kind of unknown element in JSON, because I rechecked the file 100 times and there is no "<" . And the most important thing is that I uploaded the file to github and inserted the link into fetch, everything works, but not with the local file (.
Please tell me what is my mistake.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2020-08-20
@Mothersprogrammer

Throw your JSON in publicand then just fetch('/json_name.json').

D
Dima Pautov, 2020-08-20
@bootd

Because fetch doesn't understand the address of your file, it's not clear to it what @ is.
@ is just an alias in vue cli for local development, which will then be converted to the normal path. You can't do ajax requests like this. Be sure to specify the path to the file.
fetch('../../../todo.JSON')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question