2

i'm trying to redirect to new page on clicking the link, but it redirect to new page it also add previous page url in the address bar. Kindly have a look at my code. How do i remove current page url on the upcoming page or next page.

This my HTML.

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

</div>

Here is my Script.

  <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>

On Redirection the next page url becomes like this.

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

Rather it should be

http://localhost:36547/Product/ProductDetail/102
  • 1
    https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls – Luke May 31 '17 at 09:17
  • Possible duplicate of [Absolute vs relative URLs](https://stackoverflow.com/questions/2005079/absolute-vs-relative-urls) – Luke May 31 '17 at 09:23

1 Answers1

2

You need to add slash / before product url to route from domain root

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);

Without it browser URL will be relative to the current path

hmnzr
  • 1,390
  • 1
  • 15
  • 24