Skip to main content
added 117 characters in body
Source Link
Paul T. Rykiel
  • 879
  • 1
  • 10
  • 26

Update: this is what my url is trying to resolve to:

/home/about us/api/sitecore/findanaffiliate/getaffiliate/

Update: this is what my url is trying to resolve to:

/home/about us/api/sitecore/findanaffiliate/getaffiliate/

Source Link
Paul T. Rykiel
  • 879
  • 1
  • 10
  • 26

Why is my Ajax url found when I test in preview mode, but not when I bring up the page in a url I get not found errors

so here is my Ajax :

<script type="text/javascript">
    $("#AffiliateList").change(function () {
        var textval = $("AffiliateList :selected").text();
        var selected = $("#AffiliateList :selected").val();
    
        $.ajax({
            type: "GET",
            url: "api/Sitecore/FindAnAffiliate/GetAffiliate/",
            data: {id: selected},
            content: "html",
            success: function (response) {
                
                $("#affiliateDetail").empty();
                $("#affiliateDetail").append(response);
            },
            error: function (jqHRX, textStatus, errorThrown) {
                alert(jqHRX);
                alert(errorThrown);
                alert(textStatus);
            }
            
            
            
            })
    })
</script>

when I run the this in Sitecore preview mode everything executes fine, when I type the url into my browser and bring up the page... the java script is being executed, but the url is getting a 400 level error. I am running in the same environment.

here is the controller: findAnAffiliate is NOT being hit unless I am in Preview mode:

namespace Website.Controllers
{
    public class FindAnAffiliateController : Controller
    {
        // GET: findAnAffiliate
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult FindAnAffiliate()
        {
            var dataSourceId = RenderingContext.CurrentOrNull.Rendering.DataSource;
            var dataSourceItem = Sitecore.Context.Database.GetItem(dataSourceId);

            var model = new AffiliateViewModel();
            model.title = dataSourceItem.Fields["Title"].Value;
            model.description = dataSourceItem.Fields["Description"].Value;
            model.affiliateList = model.affiliates();

            var selectedItem = model.affiliateList.FirstOrDefault();
            var affiliateItem = Sitecore.Context.Database.GetItem(selectedItem.Value);

            var affiliateDetail = new Affiliate();

            Sitecore.Data.Fields.MultilistField SponsorLinks = affiliateItem.Fields["Sponsors"];
            var sponsorLinkItems = SponsorLinks.GetItems();

            var count = 0;
            foreach (Sitecore.Data.Items.Item item in sponsorLinkItems)
            {


                var ImageLogo = (Sitecore.Data.Fields.ImageField)item.Fields["Image"];
                var ImageLogoUrl = "";
                if (ImageLogo.Value != "" && ImageLogo != null)
                {
                    ImageLogoUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(ImageLogo.MediaItem);
                }
                var linkName = item.Fields["Link Text"].Value;
                var ImageUrl = (Sitecore.Data.Fields.LinkField)item.Fields["Link"];
                var friendlyLinkUrl = ImageUrl.GetFriendlyUrl();

                if (count == 0)
                {
                    affiliateDetail.sponsor1_ImageUrl = ImageLogoUrl;
                    affiliateDetail.sponsor1_linkText = linkName;
                    affiliateDetail.sponsor1_linkUrl = friendlyLinkUrl;
                }

                if (count == 1)
                {
                    affiliateDetail.sponsor2_ImageUrl = ImageLogoUrl;
                    affiliateDetail.sponsor2_linkText = linkName;
                    affiliateDetail.sponsor2_linkUrl = friendlyLinkUrl;
                }

                count++;
            }

            affiliateDetail.title = affiliateItem.Fields["Affiliate Name"].Value;
            var linkUrl =(Sitecore.Data.Fields.LinkField) affiliateItem.Fields["Affiliate Link"];
            affiliateDetail.linkText = linkUrl.Text != "" ? linkUrl.Text : "No Link Available";
            affiliateDetail.linkUrl = linkUrl.GetFriendlyUrl();
            affiliateDetail.description = affiliateItem.Fields["Description"].Value;
            model.affiliate = affiliateDetail;


            return View(model);
        }

        public ActionResult GetAffiliate(string id)
        {
            var model = new Affiliate();

            var affiliateItem = Sitecore.Context.Database.GetItem(id);

            Sitecore.Data.Fields.MultilistField SponsorLinks = affiliateItem.Fields["Sponsors"];
            var sponsorLinkItems = SponsorLinks.GetItems();

            var count = 0;
                foreach (Sitecore.Data.Items.Item item in sponsorLinkItems)
                {
                
                
                    var ImageLogo = (Sitecore.Data.Fields.ImageField)item.Fields["Image"];
                    var ImageLogoUrl = "";
                    if (ImageLogo.Value != "" && ImageLogo != null)
                    {
                        ImageLogoUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(ImageLogo.MediaItem);
                    }
                    var linkName = item.Fields["Link Text"].Value;
                    var ImageUrl = (Sitecore.Data.Fields.LinkField)item.Fields["Link"];
                    var friendlyLinkUrl = ImageUrl.GetFriendlyUrl();
                  
                if (count == 0)
                {
                    model.sponsor1_ImageUrl = ImageLogoUrl;
                    model.sponsor1_linkText = linkName;
                    model.sponsor1_linkUrl = friendlyLinkUrl;
                }

                if (count == 1)
                {
                    model.sponsor2_ImageUrl = ImageLogoUrl;
                    model.sponsor2_linkText = linkName;
                    model.sponsor2_linkUrl = friendlyLinkUrl;
                }

                count++;
                }
             

            model.title = affiliateItem.Fields["Affiliate Name"].Value;
            var linkUrl = (Sitecore.Data.Fields.LinkField)affiliateItem.Fields["Affiliate Link"];
            model.linkText = linkUrl.Text != "" ? linkUrl.Text : "No Link Available";
            model.linkUrl = linkUrl.GetFriendlyUrl();
            model.description = affiliateItem.Fields["Description"].Value;
            return PartialView(model);
        }
    }
}

Lastly, here is the View

@model Website.ViewModels.AffiliateViewModel

<div class="container">

    <div class="headline-container">
        <div class="headline-lines"></div>
        <h2 class="text-center">@Model.title</h2>
        <div class="headline-lines"></div>
    </div>

    <div class="row">
        <div class="col-sm-6">
            <p>@Model.description</p>
        </div>

        <div class="col-sm-6">
            @Html.DropDownList("AffiliateList", Model.affiliateList as SelectList, "", new { @id = "AffiliateList", @class = "form-control input-lg" })
        <div id="affiliateDetail">
            <h4>@Model.affiliate.title</h4>
            <p class="affiliateLink"><a href="@Model.affiliate.linkUrl">@Model.affiliate.linkText</a></p>
            <p>@Model.affiliate.description</p>

            <div class="col-xs-6">
                <img class="img-responsive" src="@Model.affiliate.sponsor1_ImageUrl">
                <p class="affiliateLink small_p"><a href="@Model.affiliate.sponsor1_linkUrl">@Model.affiliate.sponsor1_linkText</a></p>
            </div>

            <div class="col-xs-6">
                <img class="img-responsive" src="@Model.affiliate.sponsor2_ImageUrl">
                <p class="affiliateLink small_p"><a href="@Model.affiliate.sponsor2_linkUrl">@Model.affiliate.sponsor2_linkText</a></p>
            </div> 

        </div>

    </div><!--row-->



</div><!--container-->
      </div><!-- /content container-->    
<script type="text/javascript">
    $("#AffiliateList").change(function () {
        var textval = $("AffiliateList :selected").text();
        var selected = $("#AffiliateList :selected").val();
    
        $.ajax({
            type: "GET",
            url: "api/Sitecore/FindAnAffiliate/GetAffiliate/",
            data: {id: selected},
            content: "html",
            success: function (response) {
                
                $("#affiliateDetail").empty();
                $("#affiliateDetail").append(response);
            },
            error: function (jqHRX, textStatus, errorThrown) {
                alert(jqHRX);
                alert(errorThrown);
                alert(textStatus);
            }
            
            
            
            })
    })
</script>