6

I am using Sitecore 8.2 with MVC.

I am trying to perform a page redirect, but my code isn't working.

I'm calling an MVC controller action method from JavaScript using a jQuery AJAX call.

The method is hit and the related items are also present in the content tree. But nothing happens. The current page remains and no redirection occurs. Also, there are no errors or messages in the browser console.

Controller method:

public ActionResult RedirectPage(string currentItemGuid)
{
  Item pageItem = Sitecore.Context.Database.GetItem(currentItemGuid);

  if (pageItem != null)
  {
    string pageURL = Sitecore.Links.LinkManager.GetItemUrl(pageItem);
    //pageURL = /products/radio
    string newURL = pageURL + "-type";
    //newURL = /products/radio-type. This item is present in the content tree in the same folder structure.
    return Redirect("~" + newURL);
  }
  else
  {
    return new EmptyResult();
  }
}

JavaScript:

$(function () {        
  $(".ddlProducts").change(function () {            
    $.ajax({
      url: "api/sitecore/Products/RedirectPage",
      type: "POST",
      data: { currentItemGuid: '{100-100-100}'},
      context: this,
      success: function (data) {                
          console.log("success", data);
      },
      error: function (data) {
          console.log("error", data);
      }
    });
  });
});
  • 1
    Please check what response you see in the Network tab of Chrome Development Tools. Find the AJAX request in the Network tab, click on it and see what is contained in the response headers. – Dmytro Shevchenko Dec 16 '16 at 7:47
  • 1
    It says 200 ok for status, the type is xhr and the initiator there is the url - http:mysite.org/api/sitecore/product/redirectpage - redirect – Qwerty Dec 16 '16 at 7:53
  • 1
    @Qwerty Have you tried debugging the method? It looks like it always goes to the else branch. Probably, the item is not found. – Dmytro Shevchenko Dec 16 '16 at 8:05
  • 1
    @DmytroShevchenko No. I checked that. The pageItem is not null and it hits the return Redirect with new url. – Qwerty Dec 16 '16 at 8:08
  • 1
    Can you decorate your controller with [AllowAnonymous]? Just to try, I don't know if it helps. – Vlad Shpak Dec 16 '16 at 8:24
6

The problem here is that 301/302 redirects are handled by the browser (or more specifically, by the XMLHttpRequest object) transparently. This means that your success handler will be called only when the browser issues a second request and loads the page it was redirected to.

So you should not attempt to use redirect response codes in AJAX calls. Instead, I suggest that you return a custom object containing the redirect URL:

return Json(new { redirectUrl = someUrl });

And then manually redirect using JavaScript:

$.ajax({
    url: "/api/sitecore/Products/RedirectPage",
    type: "POST",
    data: { currentItemGuid: '{100-100-100}'},
    context: this,
    dataType: "json", // this is important
    success: function (data) {                
        if(data.redirectUrl) {
            window.location.href = data.redirectUrl;
        }
    },
    ...
});
  • This is only working for the first instance. I have a dropdown in both pages and changing the value should redirect to respective pages. When I browse page1 and change the dropdown, it redirects to page2. Then if I change dropdown in page2, it gives an error. When I browse page2 separately and change dropdown, it redirects to page1 fine. What could be causing this. The error is – Qwerty Dec 16 '16 at 9:53
  • http://mysite.org/sitecore/service/notfound.aspx?item=%2fapi%2fsitecore%2fproducts%2fredirectpage&user=sitecore%5cadmin&site=mysite.org – Qwerty Dec 16 '16 at 9:53
  • @Qwerty Is this error something you see in the browser? Are you being redirected to /api/sitecore/Products/RedirectPage?.. – Dmytro Shevchenko Dec 16 '16 at 10:26
  • I have updated the question with more details. Kindly check. – Qwerty Dec 16 '16 at 11:07
  • 1
    @Qwerty you will need to debug your JavaScript code and find out where the wrong redirection URL comes from. This is outside of the scope of this thread. If you can't find the problem by yourself, please post a new question. If you elect to do so, please make sure to provide a minimal, complete and verifiable example. – Dmytro Shevchenko Dec 16 '16 at 11:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.