0

When connecting a website via the Sitecore Federated Experience Manager I was prompted to add a script to the destination site:

<script src="//linkedwebsite.com/bundle/beacon"></script>

After adding the script, I receiving the following error on all the pages the script was added to:

XMLHttpRequest cannot load http://mywebsite.com/sitecore/api/ssc/Beacon/Service/beacon/trackPageVisit/?co…=&sessionId=&page=http%3A%2F%2Flinkedwebsite.com%2F&referrer=&rt=1501507187250. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://linkedwebsite.com' is therefore not allowed access.

I found this Sitecore article which detailed two possible sources of the error:

https://kb.sitecore.net/articles/150778

The first was EnableCors() was not being called before GlobalConfiguration.Configure(...) in a custom pipeline. I noticed that we had in fact called GlobalConfiguration.Configure(...) without calling EnableCors() so I added the EnableCors call before the Configure call, restarted IIS, but continued to receive the same error.

The next possible source of error was the use of IIS redirects. The site I'm working on makes extensive use of these redirects so it was tough to tell if this configuration was causing a problem. To test, I removed all IIS redirects from the site but still received the XMLHttpRequest error.

The linked website as well as my Sitecore website are being run on my local IIS server. I searched through our source code and found no additional places that GlobalConfiguration.Configure(...) is called. I also tracked the GlobalConfiguration.Configuration variable in the debugger and it appears that the changes EnableCors() makes to the object remain throughout the Sitecore session (I set a breakpoint in one of the controller renderings to test this). Any ideas on additional steps I could take to test or resolve this error? Thank you for your time and help!

EDIT 1:

Here is the response header when I hit the FXM API at the error location and the JSON that is returned:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json
Expires: -1
Server: Microsoft-IIS/10.0
Set-Cookie: sc_expview=0; path=/
P3P: CP="CURa ADMa DEVa TAIi PSAi PSDi IVAi IVDi CONi HISa TELi OUR IND DSP CAO COR"
Set-Cookie: SC_ANALYTICS_GLOBAL_COOKIE=7730eccae6524d60adcc7b822c28f8a6|True; domain=.mywebsite.com; expires=Sat, 31-Jul-2027 15:18:10 GMT; path=/; HttpOnly
X-UA-Compatible: IE=edge
Date: Mon, 31 Jul 2017 15:18:10 GMT
Content-Length: 198

{"ContactId":"7730eccae6524d60adcc7b822c28f8a6|True","SessionId":"yvcffcceb1j4asjxsv3j2zfj","ContactExpires":"2019-07-31T09:14:10.648051","SessionPath":"/","ElementMatches":[],"Id":null,"Url":null}

EDIT 2:

I've removed all custom routing relevant to the api path, for example:

RouteTable.Routes.MapHttpRoute(
            "DefaultApiRoute",
            "api/{controller}/{action}");

However, I continued to experience the same error.

  • do you have some custom registration routes? – Vlad Iobagiu Jul 31 '17 at 15:16
  • Could you run an HTTP request with that URL (the http://mywebsite.com/sitecore/api one) with Postman/curl/etc. and add what the response headers are? – Matthew FitzGerald-Chamberlain Jul 31 '17 at 15:16
  • Yes, there are a few places where I see custom route registration. Some custom routing for addresses starting with api/mywebsite/admin and this custom route that may be interfering: RouteTable.Routes.MapHttpRoute( "DefaultApiRoute", "api/{controller}/{action}"); – c-bro Jul 31 '17 at 15:29
  • can you add your custom routes in this way ? <processor type="yournamespace.RegisterHttpRoutes, yourassembly" patch:after="*[@type='Sitecore.Services.Infrastructure.Sitecore.Pipelines.ServicesWebApiInitializer, Sitecore.Services.Infrastructure.Sitecore'']" /> – Vlad Iobagiu Jul 31 '17 at 15:32
  • I just tried removing the custom routing altogether but that didn't seem to make a difference. – c-bro Jul 31 '17 at 16:27
0

In addition to using FXM, we also have Sitecore Media Framework installed. In Sitecore.MediaFramework.config I noticed there was a patch for WebAPI processor. I decompiled this processor and found the following:

using Sitecore.Pipelines;
using System;
using System.Web.Http;

namespace Sitecore.MediaFramework.Pipelines.Initialize
{
  public class WebApiInitializer
  {
    public void Process(PipelineArgs args)
    {
      GlobalConfiguration.Configure(new Action<HttpConfiguration>(this.Configure));
    }

    protected void Configure(HttpConfiguration configuration)
    {
      configuration.Routes.MapHttpRoute("Reports", "sitecore/api/mediaframework/reports/{datasource}/{sitename}", (object) new
      {
        controller = "Reports",
        action = "Get"
      });
    }
  }
}

This processor occurs upstream of ServicesWebApiInitializer and does not call EnableCors(). I wrote a patch to replace this processor with my own version that calls the EnableCors() method. With the EnableCors() call in place before both in our own call to GlobalConfiguration.Configure(...) and in the call made by the Media Framework, FXM stopped displaying the XMLHttpRequest error.

The patch:

<!--Overwrite default Media Manager Web API processor with one with call to EnableCORS(). Needed for FXM functionality-->
        <processor patch:instead="processor[@type='sitecore.mediaframework.pipelines.initialize.webapiinitializer, sitecore.mediaframework']" type="MyWebsite.MediaFramework.Pipelines.Initialize.WebApiInitializer, MyWebsite" />

The code:

using Sitecore.Pipelines;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace MyWebsite.MediaFramework.Pipelines.Initialize
{
    public class WebApiInitializer
    {
        public void Process(PipelineArgs args)
        {
            GlobalConfiguration.Configuration.EnableCors();
            GlobalConfiguration.Configure(new Action<HttpConfiguration>(this.Configure));
        }

        protected void Configure(HttpConfiguration configuration)
        {
            configuration.Routes.MapHttpRoute("Reports", "sitecore/api/mediaframework/reports/{datasource}/{sitename}", (object)new
            {
                controller = "Reports",
                action = "Get"
            });
        }
    }
}

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.