从js读取/写入json到服务器上的文件(不是服务器应用程序) [英] Read/write json from js to file on server (not server app)

查看:923
本文介绍了从js读取/写入json到服务器上的文件(不是服务器应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用.js客户端,并且有对象,我需要写出到服务器上的文件。几个问题 - 使用JavaScript文件I / O对我来说是新的...我正在计划使用jQuery和JSON。我正在使用java serverside。我没有问题从我的servlet中读取,但文件I / O正在杀死我!几个问题:


  1. 我可以通过$ .ajax调用通过.js生成自己的文件,但它不处理我的JSON语法(我试过$ .getJson和$ .ajax - 手写的JSON,所以我可能(可能)做了错误的事情)。我用萤火虫的控制台,它看起来不错...

  2. 如何写我的对象到服务器上的文件?

  3. 然后,当我想阅读它时,我需要做些什么来处理它?现在我使用了一个jsonFilter函数(如果可用的话,使用JSON.parse,否则eval)来处理从servlet获得的数据。

  4. >我正在写的对象并不简单,但它也不是超级复杂的。有一个数组包含一个数组,但是如果软件正在读/写它,这应该没有什么区别。




感谢您的帮助!我很茫然 - 尝试了很多不同的东西。

解决方案


  1. 通过查询文件并将其加载到JSON对象中,通过ajax打开位于服务器上的文件。您可能需要 LINT 您的JSON


  2. 您无法通过客户端向服务器上的对象写入数据。这是一个严重的安全漏洞。
    通常的做法是更改JSON数据,然后通过ajax将其发送到服务器端代码。然后服务器将执行文件IO。

  3. 是使用JSON.parse否则eval确实是正确的。我会推荐 json2.js


  4. 数据只要通过JSONLint就可以。

你的主要问题是不可能从客户端写入服务器。让客户端通过ajax加载数据,然后查询服务器来更新文件。


I'm working with a .js client and have and object that I need to write out to a file on the server. A couple of questions - file i/o with JavaScript is new to me... I was planning on using jquery and json. I'm using java serverside. I don't have a problem reading what I get back from my servlet, but the file i/o is killing me! A couple of questions:

  1. I can open a file I generated myself via the .js with an $.ajax call, but it's not handling my json syntax (I tried both an $.getJson and $.ajax - handwritten json, so I might (probably) are doing something wrong with it). I used firebug's console and it looks ok...

  2. How can I write my object to a file on the server?

  3. Then, when I want to read it, what do I need to do to process it? Right now I'm using a jsonFilter function (uses JSON.parse if that's available, otherwise eval) to process data that I'm getting from the servlet.

  4. The object I'm writing isn't simple, but it's not super complex either. There's an array that contains an array, but that shouldn't make a difference if the software is both reading/writing it.

Thanks for any help! I'm at a loss - tried alot of different things.

解决方案

  1. You can open a file located on the server via ajax by querying the file and loading it into a JSON object. You might want to LINT your JSON

  2. You can not write to an object on the server via the client. This is a severe security breach. Common practice is to change the JSON data and then send it via ajax to server-side code. The server will then do the file IO.

  3. Yes using JSON.parse otherwise eval is indeed correct. I would recommend json2.js

  4. The data should be fine as long as it passes JSONLint.

Your main issue is that it's impossible to write to the server from the client. Get the client to load the data through ajax change it and then query the server to update the file.

这篇关于从js读取/写入json到服务器上的文件(不是服务器应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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