【问题标题】:on clicking the link current page url is also added to the upcoming page on redirection单击链接时,当前页面 url 也将添加到即将到来的重定向页面
【发布时间】:2017-11-01 00:11:30
【问题描述】:

我试图在单击链接时重定向到新页面,但它会重定向到新页面,它还会在地址栏中添加上一页 URL。请看看我的代码。如何删除即将到来的页面或下一页上的当前页面 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

【问题讨论】:

标签: javascript jquery asp.net-mvc api


【解决方案1】:

您需要在产品 url 之前添加斜杠 / 以从域根路由

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 将相对于当前路径

【讨论】:

  • 这真的应该是一个评论。
  • @Luke 当然,我为此添加了解释
  • @SaqibMajeed 很高兴它有效!请不要忘记接受答案
  • @hmnzr 问题是我担心这个问题/答案将来不会对任何人有用。
  • @hmnzr 我肯定会的。你节省了我的时间。我试图从 3 小时内解决这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 2014-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多