简体   繁体   中英

Vuejs unable to access local json file using axios. 404 (Not Found)

After trying working around it a lot to fetch JSON data from the local file.
Always receiving the following error: GET http://localhost:8080/test.json 404 (Not Found) .
The project has been created using vue-cli.

File has been kept under public folder -
在此处输入图像描述

File content is -

{
    "content": "SwiftUI was introduced in iOS 13 in a time many of us have a big app built with UIKit. SwiftUI"
}

Template -

<template>
  <div>
    {{blogcontent.content}}
  </div>
</template>

Script -

<script>
 
import axios from "axios";
const ax = axios.create({
  baseURL: "http://localhost:8080/",
});

export default {
  name: "BloePage",
  data() {
    return {
      blogcontent: { content: "Loading..." },
    };
  },
  mounted() {

    let url = "test.json";
    ax.get(url)
      .then((response) => {
        this.blogcontent = response;
        console.log(response);
      })
      .catch((err) => {
        console.log(err);
      });
  }
};
</script>

I have just edited this part of your code:

this.blogcontent.content = response.data.content;

and it works fine

<template>
  <div>
    {{blogcontent.content}}
  </div>
</template>

<script>
 
import axios from "axios";
const ax = axios.create({
  baseURL: "http://localhost:8080/",
});

export default {
  name: "BloePage",
  data() {
    return {
      blogcontent: { content: "Loading..." },
    };
  },
  mounted() {

    let url = "test.json";
    ax.get(url)
      .then((response) => {
        this.blogcontent.content = response.data.content;
        console.log(response.data.content);
      })
      .catch((err) => {
        console.log(err);
      });
  }
};
</script>

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