简体   繁体   中英

Ajax and History.js sends wrong referrer

I have a Ajax site that works with jQuery and History.js, when a link is clicked I execute this:

var $this = $(this), // $(this) is the link
url = $this.attr('href'),
title = $this.attr('title') || null;
History.pushState(null, title, url);
event.preventDefault();

Then I look the statechange for make the ajax call and change the page content:

$(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
        }
});

But the Referrer header is always the URL which is being requested, so for example, I click aa link with href "/test" on my page "/home", then the code will send the ajax call to /test, but in that request it send as referrer the same "/test" and no "/home".

I trie to make the ajax call after the pushState, and it work fine it sends the referrer fine, but the back button just stop working, it change the url but not the content so, i need to look for statechange again.

What can I do?

Sorry for my english and thanks!

Never mind, my solution was: First make the ajax call, then the History.pushState on success as I said before, but to solve the back/forward button just need listen to 'popstate' and not 'statechange'. 'popstate' was the solution.

Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM