Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/469.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 Ajax和History.js发送错误的引用_Javascript_Jquery_Ajax_History.js - Fatal编程技术网

Javascript Ajax和History.js发送错误的引用

Javascript Ajax和History.js发送错误的引用,javascript,jquery,ajax,history.js,Javascript,Jquery,Ajax,History.js,我有一个使用jQuery和History.js的Ajax站点,当单击链接时,我执行以下操作: var $this = $(this), // $(this) is the link url = $this.attr('href'), title = $this.attr('title') || null; History.pushState(null, title, url); event.preventDefault(); 然后我查看statechange以进行ajax调用并更改页面内容:

我有一个使用jQuery和History.js的Ajax站点,当单击链接时,我执行以下操作:

var $this = $(this), // $(this) is the link
url = $this.attr('href'),
title = $this.attr('title') || null;
History.pushState(null, title, url);
event.preventDefault();
然后我查看statechange以进行ajax调用并更改页面内容:

$(window).bind('statechange', function () {
     var State = History.getState(),
     url = State.url
     $.ajax({
        url: url,
        success: function (data, textStatus, jqXHR) {
           // I change the content etc etc
        }
});
但是referer标头始终是被请求的URL,因此,例如,我在我的页面“/home”上单击一个带有href“/test”的链接,然后代码将向/test发送ajax调用,但在该请求中,它作为referer发送相同的“/test”和no“/home”

我尝试在pushState之后进行ajax调用,它工作得很好,它发送referer很好,但是后退按钮停止工作,它更改了url,但没有更改内容,因此,我需要再次查找statechange

我能做什么


对不起我的英语,谢谢

没关系,我的解决方案是:首先调用ajax,然后调用History.pushState,正如我前面所说的那样,如果要解决后退/前进按钮,只需听“popstate”,而不是“statechange”“popstate”是解决方案


尊敬。

$.ajax()
允许手动设置请求标头-请参阅-以便您可以尝试将referer标头设置为
/home
。谢谢@Roamer-1888,我也看到了此选项,但我看到的是,出于安全原因,浏览器将覆盖referer。阅读这里:所以我不测试它。在这种情况下,您似乎不能依赖“statechange”处理程序来发出AJAX请求。尝试将流程从内到外转换-进行AJAX调用,然后在成功处理程序中执行
History.pushState()
。再次感谢@Roamer-1888,我尝试过了,它可以工作,但正如我所说,后退按钮停止工作,因为当用户按下浏览器的后退按钮时,它会执行“状态更改”。。。因此,我需要知道用户何时返回或前进,而不是执行AJAX调用,有什么问题吗?谢谢我认为大多数人最终都会更改url的散列字符串,而不是创建历史记录条目。很高兴这对您的用例有效,但我还是要提醒您,一般不要使用这种方式构建。最终,你会有很多额外的逻辑,这相当于“检查并查看这是否是我推过的状态,或者这是否是浏览器基于后退/前进单击推过的相同状态。”而发送该信息看起来方便的地方是状态本身,但当它是推过的状态时,它将欺骗你!在我看来,“Referer”头是不值得的——我只会将应用程序更改为接受Referer参数(或自定义头),并使用它而不是头。