2017-05-31 57 views
2

我試圖重定向到新頁面點擊鏈接,但它重定向到新的頁面,它也在地址欄中添加前一頁的網址。請看看我的代碼。我如何刪除即將到來的頁面或下一頁上的當前頁面網址。點擊鏈接當前頁面的URL也被添加到即將推出的頁面上重定向

這是我的HTML。

@{ 
    ViewData["Title"] = "ProductList"; 
} 
<script src="~/lib/jquery/dist/jquery.min.js"></script> 
<h2>Product List</h2> 
<div id="im" > 

</div> 

這是我的腳本。

<script> 
     $(document).ready(function() { 
      var loc = window.location.href; 
      var dat = loc.substring(loc.lastIndexOf('/') + 1); 
      $.ajax({ 
       url:'api-url' 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       data: JSON.stringify({ "ProjectId": "1", "CategoryId": dat }), 
       success: function (data) 
       { 
        console.log(data); 
        $.each(data.ProductList, function() { 
         var list = "<a href='Product/ProductDetail/" + this.Id + "'><img src=" + this.ImageLink + " height=400 width=400> " + this.Name + "</a>&nbsp; &nbsp; $" + this.Price + " </img> "; 
         $("#im").append(list); 
        }) 
       } 
      }); 
     }); 
    </script> 

在重定向上,下一個頁面url變成這樣。

http://localhost:36547/Product/ProductList/Product/ProductDetail/102 

相反,它應該是

http://localhost:36547/Product/ProductDetail/102 
+1

https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls – Luke

+0

[絕對vs相對URL]的可能重複(https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls) – Luke

回答

2

你需要從域根

var list = "<a href='/Product/ProductDetail/" + this.Id + "'><img src=" + this.ImageLink + " height=400 width=400> " + this.Name + "</a>&nbsp; &nbsp; $" + this.Price + " </img> "; 
$("#im").append(list); 

產品網址前加斜槓/路由沒有它的瀏覽器URL將相對於當前路徑

+0

偉大的男孩這個作品。非常感謝。 –

+0

這真的應該是一個評論。 – Luke

+0

這是一個可行的解決方案+ 1 –

相關問題