简体   繁体   English

如何在客户端保存JSON文件并在以后检索?

[英]How to save a JSON file on the client side and retrieve it later?

Using PHP, I want to create a JSON file with say firstname, lastname. 使用PHP,我想创建一个名字为lastname的JSON文件。

After I use json_encode , how can I save it to the client and most importantly, how do I retrieve it and read it? 使用json_encode ,如何将其保存到客户端,最重要的是,如何检索和读取它?

Use local storage 使用本地存储

Or you could simply store the information in a cookie, but that may raise security issues. 或者,您可以仅将信息存储在cookie中,但这可能会引发安全问题。 Plus you'd be sending that data back and forth on each request on your domain (html, css, images, etc.) which can slow down website performance. 另外,您还会根据域中的每个请求(html,css,图像等)来回发送该数据,这可能会降低网站的性能。

If you need to just populate the auto-complete why do you need the file? 如果只需要填充自动完成功能,为什么需要该文件? Implement some RESTful api in the server-side that outputs a json and then consume the json in javascript using: 在服务器端实现一些RESTful API,该API输出json,然后使用以下代码在javascript中使用json:

var JSONObject = eval('(' + data + ')');
alert(JSONObject.property1);

Here data is the json encoded string returned by the server-side. 这里的data是服务器端返回的json编码的字符串。

If you really must save it to file, then I think cookies are the only option you have. 如果您确实必须将其保存到文件中,那么我认为cookie是您唯一的选择。

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

相关问题 有没有办法从特定文件中的 contenteditable 数据框中保存和检索文本? (所有客户端) - Is there a way to save and retrieve text from contenteditable data box in a specific file ? (All client side) 如何将 HTML 表格保存为草稿,然后检索并填写 rest? - How to save an HTML form as draft and then retrieve and fill the rest on later time? 如何在响应客户端之前将整个http响应保存到服务器端的日志文件 - How to save entire http response to log file on server side just before responsing to the client 如何在服务器端拆分JSON,然后在客户端加入并流式传输 - How to split JSON in server side then join and stream it in client side 如何从客户端修改存储在服务器上的 a.JSON 文件? - How can I modify a .JSON file stored on server from client side? 如何创建RESTfull服务以从客户端检索json或xml文件 - How can I create a RESTfull service to retrieve a json or xml file from a client 在客户端将PHP转换为JSON - PHP to JSON on client side 如何使用PHP删除客户端文件? - How to delete a client side file using PHP? 如何取回<textarea> POST 值并保存到文件? - How to retrieve <textarea> value with POST and save to a file? 如何获取升级文件的客户端路径 - how to get client side path of the uploded file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM