How to access json file in src folder created usin

2019-08-30 08:36发布

I'm a newbie to react. I have created react application using npx create-react-app. In actions,axios.get is throwing localhost:3000 file not found error. Below is the code which is in src/actions/index.js

       export function lang(language) {
      let url = "./../resources/strings/strings_" + language + ".json";
      return dispatch => {
        axios.get(url).then(response => {
          console.log(response.data)
        }).catch(response => {
          console.log(response);
        });
      }
    }

This is the project structure [1]: https://i.stack.imgur.com/CwTJg.png

What am I doing wrong? I couldn't find a proper solution for this. Thanks in advance.

1条回答
欢心
2楼-- · 2019-08-30 08:56

I agree with Easwar, you need to look at the answer to that question.

It appears axios only operates in the /public folder so you would need your json files in the public folder. You could put them within a folder in the public folder if you wanted.

This makes sense because the webpack bundle is in the public folder and once the app is bundled only files within the public folder are available to your react project.

Since axios is an xhr request library at the moment you use it the only file structure it can possibly see is in the public folder so most likely: index.html, manifest.json, and your webpack bundle.js which has all your js code bundled into 1 file.

查看更多
登录 后发表回答