php - 我如何将 json 从 php 传递到 js 并对其进行操作?

标签 php javascript json caching

这就是我想做的

  1. 我使用 codeigniter directory_map() 函数映射文件夹,将其保存为 $map;

     $map = directory_map($this->rootdir.$this->session->userdata('user_name'));
    
  2. 然后我对 $map 进行编码并回显它

     echo $dir = json_encode($map);
    
  3. 我想要的是以某种方式保存 $dir 。所以当用户打开其中一个文件夹时,我可以使用 js 从保存的 $dir json 文件中获取文件夹的内容,而不是与服务器通信。如果这使得那么我该如何保存 $dir json 文件或如何将其获取到 js 文件中。

有更好的方法吗?

最佳答案

我个人使用提到的方法here ,基本上是沿着以下轮廓:

更新: (我想我让你有点困惑)以下是 Javascript 代码

//Global object to store the cache
cache = {};
function makeRequest(requested_data){
   request_hash = hash(requested_data);
   if (cache[request_hash]){
       handleData(cache[request_hash]);
   }
   else{
       //Make Ajax call, get data response back and store it in the cache
       request_hash = hash(requested_data);
       cache[request_hash] = data;
       handleData(data);
   }
}

注意:对于 hash(),您可以使用任何散列算法,您实际上并不需要加密级别的散列,只需创建一个基本签名。我用Javascript implementation of Java's hashCode()

关于php - 我如何将 json 从 php 传递到 js 并对其进行操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11446728/

相关文章:

php - PDO 获取 id 和名称

javascript - 使用按钮和 javascript 在 Flask 中设置 python 变量

javascript - 正则表达式的灾难性回溯问题

javascript - Json 字符串转数组(js、jQuery)

php - 通过运行 cron 来更改访问的代码

php - PHP 中的静态类是不好的做法吗?

python - Flask 请求将 pandas 传递给_json 与 curl 或 Postman

ios - 从 iOS 中 NSDictionary 内的字典中检索值

xpath 后的 PHP DomXPath 编码问题

javascript - Node js+错误: listen EADDRINUSE + Unhandled 'error' event