Jquery history.pushState()未显示正确的URL

Jquery history.pushState()未显示正确的URL,jquery,html,pushstate,Jquery,Html,Pushstate,我正在使用jQuery和PHP,我在history.pushState方面有问题 当我单击锚定标记或链接时,浏览器中的URL如下所示www.example.com/index.php/home/viewer/id 当我再次单击链接时,浏览器中的URL如下所示 www.example.com/index.php/home/photo\u viewer/index.php/home/viewer/id 这是不正确的 我希望浏览器中的URL为www.example.com/index.php/home

我正在使用jQuery和PHP,我在
history.pushState
方面有问题

当我单击锚定标记或链接时,浏览器中的URL如下所示
www.example.com/index.php/home/viewer/id

当我再次单击链接时,浏览器中的URL如下所示
www.example.com/index.php/home/photo\u viewer/index.php/home/viewer/id

这是不正确的

我希望浏览器中的URL为
www.example.com/index.php/home/viewer/id

我如何解决这个问题

<a href="index.php/home/viewer/ $row['id'] " Onclick="viewer(this); return false;"> id </a>

<script type="text/javascript">
   function viewer(link){
       var ajax_data ={ajax:'1'};

       $.ajax({
             type: "POST",
             url: link,  
    data: ajax_data,
             success: function(html){

                  $("#viewer").html(html); 

    window.history.pushState(null,null, link);

    e.preventDefault();
     }});
        return false; } 

函数查看器(链接){
var ajax_data={ajax:'1'};
$.ajax({
类型:“POST”,
网址:link,
数据:ajax_数据,
成功:函数(html){
$(“#查看器”).html(html);
window.history.pushState(null,null,link);
e、 预防默认值();
}});
返回false;}

可能是因为您的URL是相对的。您必须通过在斜线前加上前缀使其成为绝对值:

href="/index.php/home/viewer/..."
//    ^
相对URL始终指定相对于当前资源的资源,即路径仅附加到当前路径。另见:

新的URL不需要是绝对的;如果是相对的,则相对于当前URL解析


更新:尽管它确实起到了很大的作用,但请访问链接的
href
属性,而不是链接本身:
链接。href

@用户:您说您编辑了代码,但URL看起来还是一样。。。它将使用绝对URL。