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

查看:718
本文介绍了使用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文件放在公共文件夹中,然后使用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天全站免登陆