8

In my Vue project, I have mocked some data for next step development. I already save the test data in a json file. And my vue project is typical one created with Vue-Cli, and the structure for my project goes as following:

My_project
    build
    config
    data
        service_general_info.json
    node_modules
    src
        components
            component-A
                component-A.vue

as you can see, all the folders are created by the vue-cli originally. And I make a new folder data and place the test data json file inside.

And I want to read in the data by axios library in an event handling function inside the component of component-A as following:

  methods: {
    addData() {
      console.log('add json data...');
      axios.get('./../../data/service_general_info.json');
    },
  },

I use relative path to locate the target file.But get 404 error back. So how to set the path correctly? Currently I am running the dev mode in local host.

The error message is: GET http://localhost:8080/data/service_general_info.json 404 (Not Found)

2
  • 1
    I'm afraid that you can not use axios like this! This library is for making HTTP requests but it seems you're trying to access file-system instead which won't work as far as I know. Commented Jul 27, 2017 at 11:48
  • did u get any solution ? Commented Sep 27, 2017 at 10:33

6 Answers 6

10

In Vue-cli project, axios can't get data from custom folder.

You should use static folder to save test json file.

So you should change axios call like this:

axios.get('/static/service_general_info.json');

This will get data from json.

Sign up to request clarification or add additional context in comments.

3 Comments

is the static folder assets ?
No, static is different from assets.
You saved my life!
6

If you are doing just for sake of testing then you can save it in public folder and access it directly on http root.

e.g. I have the file results.json in public folder then I can access it using http://localhost:8080/results.json

3 Comments

Yes, it is working with local but not with the build on the server.
check build output and adjust url accordingly.
Issue resolved, server not allowing .json format, just added mimeType in the web.config file and now it's working correctly. Thanks.
5

For me it didn't work using static folder. I had to put it in public folder.

I put json folder in public & then accessed it like below.

getCountries() {
    return axios.get('json/country-by-abbreviation.json', { baseURL: window.location.origin })
        .then((response) => { return response.data; })
        .catch((error) => {
            throw error.response.data;
        });
}

Comments

2

When the http call is made from the server, axios has no idea that you're on http://localhost:8080, you have to give the full url.

Like this:

methods: {
    addData() {
      console.log('add json data...');
      axios.get('http://localhost:8080/data/service_general_info.json');
    },
  },

Comments

2

I had this same issue, only the above solutions wouldn't work as it is being uploaded to a subdirectory. I found I needed to put it in the public/assets folder and use:

axios.get(process.env.BASE_URL+'assets/file.json')

While in vue.config.js I have set the local and live paths

module.exports = {
    publicPath: process.env.NODE_ENV === 'production'
    ? '/path/to/app/'
    : '/'
}

Comments

-1

You can simply read a static JSON file using import. Then assign in data.

import ServiceInfo from './../../data/service_general_info.json';
export default{
    data(){
        return {
            ServiceInfo
        }
    }
}

2 Comments

This will never work out of the box as json files do not have an export, therefore you would have to export * from the json file in order for it to work correctly.
Actually this works just fine. However it will be processed by webpack.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.