Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript获取文件修改后的时间戳_Javascript_Jquery - Fatal编程技术网

使用javascript获取文件修改后的时间戳

使用javascript获取文件修改后的时间戳,javascript,jquery,Javascript,Jquery,是否可以仅使用JavaScript获取文件的修改时间戳 我使用一个JSON文件通过javascript填充页面,我想显示该JSON文件的时间戳。如果您通过真正的ajax(即,通过XMLHttpRequest)检索文件,则可以这样做,前提是您将服务器配置为在发送数据时发送上次修改的头 这里最基本的一点是,当您使用XMLHttpRequest时,您可以访问响应头。因此,如果服务器发回上次修改的,您可以使用它: var xhr = $.ajax({ url: "data.json",

是否可以仅使用JavaScript获取文件的修改时间戳


我使用一个JSON文件通过javascript填充页面,我想显示该JSON文件的时间戳。

如果您通过真正的ajax(即,通过
XMLHttpRequest
)检索文件,则可以这样做,前提是您将服务器配置为在发送数据时发送上次修改的

这里最基本的一点是,当您使用
XMLHttpRequest
时,您可以访问响应头。因此,如果服务器发回上次修改的
,您可以使用它:

var xhr = $.ajax({
    url: "data.json",
    success: function(response) {
        display("Data is " + response.data + ", last modified: " + xhr.getResponseHeader("Last-Modified"));
    }
});
刚刚在Chrome、Firefox、IE8和IE11上试过。工作正常(即使数据来自缓存)


您在下面已经说过,您需要在循环中执行此操作,但是您会一直看到变量的最后一个值。这告诉我你做过这样的事情:

// **WRONG**
var list = /*...some list of URLs...*/;
var index;
for (index = 0; index < list.length; ++index) {
    var xhr = $.ajax({
        url: list[index],
        success: function(response) {
            display("Data is " + response.data + ", last modified: " + xhr.getResponseHeader("Last-Modified"));
        }
    });
}
由于
forEach
回调的每次迭代都会得到自己的
xhr
变量,因此没有串扰。(您需要在旧浏览器上为forEach
添加垫片。)


你说的是:


我已经考虑过一个闭包问题,这就是为什么我在循环中使用数组
xhr[e]
。。。 但是你的例子没有给我帮助

并在摘要中链接到此代码:

//loop over e....
nodename=arr[e];
node_json=path_to_node_json+nodename;
html +='<a href="'+node_json+'" target="_blank" id="host_'+nodename+'">data</a></td>'
    +'</tr>';
xhr[e] = $.ajax({
    url: node_json,
    success: function(response) {
        $('#host_'+nodename).append("last modified: " + xhr[e].getResponseHeader("Last-Modified"));
    }
});

相关问题:或者更好:@casey:如果你仔细地打电话给XHR,这些可能并不准确。。。rubo77:如果是服务器知道的创建时间,可以吗?(如果不是,除了嵌入数据之外,我真的看不到它。)@t.J.Crowder当然,这似乎足够有效(至少在同一个域上)如何将调用提交的变量放入
函数(响应)
?如果我在一个循环中调用其中的几个调用,我总是在子函数中只有最后一个值。我已经考虑了一个闭包问题,这就是为什么我在
e
上的循环中使用了数组
xhr[e]
,并且我成功地获得了所有不同URL的CAL。但是您的示例没有发送帮助,我有一个问题:我总是只获取上一个hrefI重新定义的id。下面的问题:@rubo77:那里的代码仍然存在经典的闭包问题,
forEach
修复了它。请参阅更新的答案。但所有这些都与您提出的有关查找修改时间的问题无关。我已经在此处使用arr.map()修复了它:但是谢谢您的帮助
//loop over e....
nodename=arr[e];
node_json=path_to_node_json+nodename;
html +='<a href="'+node_json+'" target="_blank" id="host_'+nodename+'">data</a></td>'
    +'</tr>';
xhr[e] = $.ajax({
    url: node_json,
    success: function(response) {
        $('#host_'+nodename).append("last modified: " + xhr[e].getResponseHeader("Last-Modified"));
    }
});
// (I assume `node_json`, `html`, and `path_to_node_json` are all declared
// here, outside the function.)
arr.forEach(function(nodename) {
    var xhr; // <=== Local variable in this specific call to the iteration
             // function, value isn't changed by subsequent iterations
    node_json=path_to_node_json+nodename;
    html +='<a href="'+node_json+'" target="_blank" id="host_'+nodename+'">data</a></td>'
        +'</tr>';
    xhr = $.ajax({
        url: node_json,
        success: function(response) {
            // Note: You haven't used it here, but just to emphasize: If
            // you used `node_json` here, it would have its value as of
            // the *end* of the loop, because it's not local to this
            // function. But `xhr` is local, and so it isn't changed on
            // subsequent iterations.
            $('#host_'+nodename).append("last modified: " + xhr.getResponseHeader("Last-Modified"));
        }
    });
});