如何使用jQuery将数据写入本地JSON文件? [英] How can I write data in local JSON file using jQuery?

查看:4828
本文介绍了如何使用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.

这篇关于如何使用jQuery将数据写入本地JSON文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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