Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dr. Sugus 26 posts 101 karma points
    May 12, 2015 @ 10:09
    Dr. Sugus
    0

    Enabling ajax POST with CORS through a UmbracoApiController

    Hi community

    I have controllers that work fine crossdomain when doing a GET. I used the following setting in web.config:

    <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>

    However, I can't seem to do a crossdomain POST the same way. When executing the POST, I get following two errors (I am a bit unsure as to whether #1 is an actual error):

    1) OPTIONS [some url here] [some controller method here] @ [someplace in POSTing js file].

    2) (index):1 XMLHttpRequest cannot load [some url here]. Invalid HTTP status code 405

    I tried installing the Microsoft.AspNet.WebApi.Cors NuGet package, but it doesn't seem compatible with my Umbraco 7.

    How does everyone else here handle CORS POST requests?

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    May 12, 2015 @ 13:05
    Alex Skrypnyk
    0

    Hi,

    Did you try config like that:

    <httpProtocol>
      <customHeaders>
        <remove name="X-Powered-By" />
        <add name="Access-Control-Allow-Origin" value="*" />
    
        <add name="Access-Control-Request-Headers" value="*" />
      </customHeaders>
    </httpProtocol>
    

    Other problem to avoid authorization header in angular request.

    Thanks, Alex

  • Dr. Sugus 26 posts 101 karma points
    May 12, 2015 @ 14:09
    Dr. Sugus
    0

    Thanks Alex - I tried that already, no luck.

     

  • Alex Skrypnyk 6131 posts 23950 karma points MVP 7x admin c-trib
    May 12, 2015 @ 14:50
    Alex Skrypnyk
    0

    Maybe this piece of code can help ?

    https://github.com/noshitsherlock/umbraco-angular

    using System.Web.Http.Filters;

    namespace UmbracoTest.Services
    {
        public class AllowCrossSiteJsonAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
            {
                if (actionExecutedContext.Response != null)
                {   
                    if(actionExecutedContext.Response.RequestMessage.RequestUri.Host.ToLower() == "localhost")
                        actionExecutedContext.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                }
    
                base.OnActionExecuted(actionExecutedContext);
            }
        }
    }
    
  • Biagio Paruolo 1583 posts 1814 karma points c-trib
    Jun 13, 2017 @ 08:10
Please Sign in or register to post replies

Write your reply to:

Draft