简体   繁体   English

如何使用 AJAX 创建 Json 数据源 .txt 文件?

[英]How could I use AJAX to create a Json data source .txt file?

I'm creating a form that collects standard information about customers.我正在创建一个表单来收集有关客户的标准信息。 When the user hits save, I would like to create a .txt file that would be used to later retrieve all of the data collected from customers.当用户点击保存时,我想创建一个 .txt 文件,用于稍后检索从客户收集的所有数据。 I'm using DataTables which is a jQuery plugin to display the data.我正在使用 DataTables,它是一个 jQuery 插件来显示数据。 The .txt file would be formatted to be saved as such: .txt 文件将被格式化为保存为:

{
  "aaData": [
    ["client 1 name", "address", "city", "state", "zip"],
    ["client 2 name", "address", "city", "state", "zip"],
    ["client 3 name", "address", "city", "state", "zip"],
    ...["client x name", "address", "city", "state", "zip"]
  ]
}

Where "aaData": is used by DataTables.其中 "aaData": 由 DataTables 使用。 This is going to part of an iPhone app, so the data source has to be very small and not reliant on a constant connection to a server, so, essentially, a client-side data source.这将成为 iPhone 应用程序的一部分,因此数据源必须非常小并且不依赖于与服务器的持续连接,因此,本质上是客户端数据源。 The .txt file has to also be updated when edited and saved, and then replaced every time it is downloaded. .txt 文件在编辑和保存时也必须更新,然后在每次下载时替换。

Do you want to save a .txt file locally (ie, on the client computer)?您想在本地(即在客户端计算机上)保存 .txt 文件吗? That's not possible: for (obvious) security reasons, a browser's JavaScript environment is unable to access the local file system.这是不可能的:出于(明显的)安全原因,浏览器的 JavaScript 环境无法访问本地文件系统。 You can save this JSON-formatted data in a cookie (max. 4 KB per cookie) or, with HTML 5, using localStorage .您可以将此 JSON 格式的数据保存在 cookie 中(每个 cookie 最多 4 KB),或者在 HTML 5 中使用localStorage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM