javascript - 为什么 "_=1389258551926"在 ajax 请求中作为查询字符串参数发送?

标签 javascript jquery ajax

我正在使用 JQuery Ajax 向我的操作类发送请求,其中包含 data: {campaignId: campaignId}_=1389258551926 也作为数据发送。

我的ajax请求函数是:

$('#submit').click(function() {
    var campaignId = $('#campaign').val();
    alert("Ajax request ; Camp : " + campaignId);
    $.ajax({
        type: "get",
        url: "getCampData",
        data: {campaignId: campaignId},
        dataType: "json"
    }).done(function(data) {
        alert("Camp List : " + data.campList);
});

查询字符串参数:

campaignId=Test&_=1389258551927

为什么这个额外的参数作为数据发送?

最佳答案

这个参数是一个时间戳。您可以看到它与您在控制台中获得的内容非常相似

Date.now()

这样做是为了确保 URL 更改并避免接收页面的缓存版本。

the documentation 中有描述:

cache (default: true, false for dataType 'script' and 'jsonp')

Type: Boolean If set to false, it will force requested pages not to be cached by the browser. Note: Setting cache to false will only work correctly with HEAD and GET requests. It works by appending "_={timestamp}" to the GET parameters. The parameter is not needed for other types of requests, except in IE8 when a POST is made to a URL that has already been requested by a GET.

关于javascript - 为什么 "_=1389258551926"在 ajax 请求中作为查询字符串参数发送?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21016070/

相关文章:

基于javascript的进度条

javascript - 如何从 jquery 对话框中返回 true 消息

javascript - 无法使用带有 babel 加载器的 web-pack 编译 JSX

javascript - 每 10 秒运行一次 php 脚本

java - 使用ajax和servlet上传文件

javascript - 从文件夹加载图像时 Ajax 调用抛出 404

javascript - 如何将当前URL参数添加到页面上的所有链接

javascript - 使用jquery重置每个文档点击的超时

javascript - 在另一个函数中触发点击函数

javascript - "getContext is not a function"为元素选择器使用变量时