如何使用jQuery将数据写入本地JSON文件?

问题描述:

我正在使用Eclipse Kepler IDE.我无法在文件夹位置JSON/a.json的本地JSON文件中写入数据.

I am using Eclipse Kepler IDE. I am unable to write the data in the local JSON file at the folder location JSON/a.json.

下面是我的代码.

$("#submitDetails").click(function(){
    var newJSONData = {};
    //alert("Function called");
    projectID=$("#projectID").val();
    managerID=$("#managerID").val();
    pocId=$("#pocId").val();
    comment=$("#comment").val();


    newJSONData = [{
        "Project" :projectID,
        "ManagerID" :managerID,
        "POC" :pocId,
        "Comment":comment
    }];


    $.ajax
    ({
        type: 'POST',
        url: 'JSON/a.json',
        data: newJSONData,
        success: function () {alert("Project Submitted!"); },
        failure: function() {alert("Error in project submission!");}
    });
});

您不能从基于浏览器的JavaScript代码直接写入本地文件系统 .如果可以的话,请考虑其中的含义. :-)

You can't write to the local file system directly from browser-based JavaScript code. Consider the implications if you could. :-)

相反,您必须在本地运行Web服务器,并拥有服务器端资源来接收数据,对请求进行身份验证和授权(例如,确保可以写入文件)并将其写出.然后将数据发布到本地服务器上该服务器端资源的URL.

Instead, you have to run a web server locally and have a server-side resource to receive the data, authenticate and authorize the request (e.g., make sure its okay to write the file), and write it out. Then post the data to the URL of that server-side resource on the local server.