使用JSON文件作为Javascript Web App的数据库 [英] Use JSON File as a Database for Javascript Web App

查看:114
本文介绍了使用JSON文件作为Javascript Web App的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以仅将JSON文件用作数据库来直接读取和写入该文件.

I was wondering if there is any way to use just a JSON file as a database to read and write straight to the file.

我正在创建一个我公司想要构建的快速Mockup Web应用程序,但他们希望看到一个MVP,以便在投入资源和时间来使用真实的数据库,节点等进行正确构建之前,先看一下它是否有用...

I'm creating a quick Mockup web app that my company wants built but they want to see a MVP to see if it will be useful before dedicating resource and time to build it properly with a real database, node etc...

暂时只是使一些基本功能正常工作,例如从JSON读取数据和填充下拉列表等...

For the time being just to get some basic features working like reading data from a JSON and populating dropdown lists etc...

我想知道是否可以设置要写入和读取的JSON文件?

I was wondering if it is possible to set up a JSON file to write to as well as read from?

目前,我像使用API​​一样从JSON中读取内容:

Currently I'm reading from the JSON like you would an API:

function loadJSON(callback) {

    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType("application/json");
    xobj.open('GET', 'pmApp.json', true);
    xobj.onreadystatechange = function () {
        if (xobj.readyState == 4 && xobj.status == "200") {
            // Required use of an anonymous callback as .open will NOT return a value but simply returns undefined in asynchronous mode
            callback(xobj.responseText);
        }
    };
    xobj.send(null);
};

// Run loadJSON - 
loadJSON(function (response) {
    var json = JSON.parse(response); // Parse JSON string into object

    appJS(json); //Run the App pass in json variable
});

很抱歉,如果重复的话,我已经搜索过,但找不到答案.

Sorry if this is a duplicate, I've searched but couldn't find an answer.

推荐答案

如果要写,不可以. 如果仅在浏览器中使用JavaScript,则无法写入文件.您将需要在后端上有一些API,您可以将写入请求发送到该API.

If you want to write to it, No. If you're just using JavaScript in the browser then you can't write to files. You will need to have some API on the backend that you can send write requests to.

如果您只是要准备用于快速演示的简单模型,而没有其他用途,则可以考虑在浏览器中使用localStorage或sessionStorage临时存储数据以进行快速演示.

If you're just making a simple mockup that you intend to use for a quick presentation and nothing else, then you could consider using localStorage or sessionStorage in the browser to temporarily store data for a quick presentation.

这篇关于使用JSON文件作为Javascript Web App的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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