使用 CGI 和 Javascript 保存文件
我有一些 JSON 脚本,我计划在网站上解析它们,然后允许客户端通过我的界面编辑它们(稍后再次加载和解析以进行显示)。问题是,Javascript 无法写入文件系统。我已经有了一个使用 Javascript(和 jQuery)读取 JSON 文件的系统。现在,我听说以后可以使用 CGI 来保存数据。有人可以给我一些参考资料和深入的解释吗?我读过一些关于 CGI 的一般知识,但没有具体的内容。
感谢您的帮助!
I have some JSON scripts that I plan on parsing on the site and then allowing the clients to edit them through my interface (to later be loaded and parsed once again for display). Problem is, Javascript doesn't have access to writing to the file system. I've already got a system for reading the JSON files using Javascript (and jQuery). Now, I've heard I can use CGI to save the data later. Can somebody give me some references and in depth explanations? I've read a bit about CGI in general but nothing specific.
Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CGI 是服务器与脚本交互的一种方式。基本上,您只需设置服务器来执行一个文件,它将通过设置多个环境变量并将 POST 数据馈送到其标准输入来执行该文件。该脚本应输出页面的标题,然后是内容。
CGI 脚本可以用多种不同的语言编写。 Perl 以 CGI 脚本而闻名; 此处有相关文档。 Python 有一个
cgi
模块来处理CGI。 Ruby 有 一个CGI
包也是如此。下面是一个用 Python 编写的快速 CGI 脚本,用于写入文件。您可能想要修改它或将其用作参考,而不是按原样使用它:
如果您使用
data
参数POST
到它,它将保存该数据数据到与其自身相同的目录中的some_file.json
中。CGI is a way for servers to interface with scripts. Pretty much, you just set up the server to execute a file, and it will execute it with several environment variables set and POST data fed to its standard input. The script should output the headers for the page, followed by the content.
CGI scripts can be written in many different languages. Perl is well-known for CGI scripts; it has documentation on it here. Python has a
cgi
module to deal with CGI. Ruby has aCGI
package as well.Here's a quick CGI script written in Python that writes to a file. You'll probably want to modify it or use it as a reference rather than using it as-is:
If you
POST
to it with adata
parameter, it will save that data intosome_file.json
in the same directory as itself.