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

use*_*850 2 jquery html5 pushstate

我正在使用jQuery和PHP,我遇到了问题history.pushState.

当我点击锚标记或链接后,浏览器中的URL看起来像这样 www.example.com/index.php/home/viewer/id

当我再次单击该链接时,浏览器中的URL看起来像这样 www.example.com/index.php/home/photo_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; } 
Run Code Online (Sandbox Code Playgroud)

Fel*_*ing 6

可能是因为您的URL是相对的.你必须通过增加一个斜杠来使它成为绝对的:

href="/index.php/home/viewer/..."
//    ^
Run Code Online (Sandbox Code Playgroud)

相对URL始终指定对于当前资源的资源,即路径仅附加到当前路径.另见文档:

新URL不一定是绝对的; 如果是相对的,则相对于当前URL进行解析.

更新:虽然它确实有很大的不同,但访问href链接的属性,而不是链接本身:link.href.