[英]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.