使用 axios 获取本地 JSON 数据时遇到问题 [英] Trouble getting local JSON data with axios

查看:96
本文介绍了使用 axios 获取本地 JSON 数据时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 axios 从本地 json 文件中获取数据.在控制台下,我什至无法得到回应,所以我想我会在这里询问.

I'm trying to get data from a local json file using axios. Under the console I can't even get a response so I figured I'd ask about it here.

var loadData;

function loadData() {
    axios({
        url: "[filepath]/json/docs.json", 
        responseType: 'json',
        credentials: "include",
        mode: "no-cors",
        headers: {
            "Accept": "application/json; odata=verbose"
        }           
    }).then((response) => {
        console.log(response.data);      
    })
}

.then((response) 行上,它告诉我我有一个语法错误---我认为它与响应语法有关,但我不太确定.有什么想法吗?

On the .then((response) line it's telling me I have a syntax error---I think it's pertaining to the response syntax but I'm not so sure. Any thoughts?

推荐答案

只需将您的 json 文件放在 public 文件夹中并使用 axios 调用它.假设您的项目的公共文件夹中有名为 data.json 的 json 文件.

Simply put your json file in public folder and call it using axios. Suppose if you have json file named called data.json in public folder of your project.

然后这样称呼它

    axios.get('data.json')
    .then(res => console.log(res.data))
    .catch(err => console.log(err)

在此之前请确保导入 axios.喜欢,

Before that make sure to import axios. like ,

import axios from 'axios';

希望有帮助.

这篇关于使用 axios 获取本地 JSON 数据时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆