Copied to clipboard

Flag this post as spam?

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


  • Nicola Nauwynck 11 posts 76 karma points
    Feb 05, 2015 @ 17:06
    Nicola Nauwynck
    0

    Issue with CORS when posting to web api

    Hello,

    I have a project that runs on Umbraco 7.1.1 and I want to post an object to a webapi in the project.

    The action is located on /umbraco/api/dummyapi/getsearchcontent with the following code

    [HttpPost]
    public dynamic GetSearchCount(Guid token, SearchCountRequest request)
    {
        // here I do some code
    }
    

    But when I post my object from Angular I get "XMLHttpRequest cannot load *. Invalid HTTP status code 405". The Angular code isn't in the same project.

    Search results on Google tell me that this is an issue with CORS so I have tried different things like:

    I have found that installing this package: https://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors should be a solution but it isn't possible because Umbraco uses Microsoft.AspNet.WebApi Version 4.. and this isn't compatible with the earliest version of Microsoft.AspNet.WebApi.Cors.

    Any suggestions on how you got CORS working? Am I missing something or?

  • Yakov Lebski 590 posts 2344 karma points
    Feb 23, 2015 @ 21:33
    Yakov Lebski
    0

    Hi I have the same issue, did you solve it ?

  • Nicola Nauwynck 11 posts 76 karma points
    Feb 24, 2015 @ 08:47
    Nicola Nauwynck
    0

    No I did not solve it but I worked around it.

    I placed the Angular code in the same project so it runs on the same localhost. Now the API and Angular are running on the same host so there are no longer CORS isssues

  • Yakov Lebski 590 posts 2344 karma points
    Feb 24, 2015 @ 10:29
    Yakov Lebski
    4

    I my case I solve it it in 2 step, add in web.config

    <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 is avoid send authorization header in angular request

  • Urvish 252 posts 776 karma points
    Apr 06, 2015 @ 10:46
    Urvish
    0

    Thanks Yakov Lebski for the solution.

    It works for me for simple ajax post.

    Regards,

    Urvish Mandaliya

Please Sign in or register to post replies

Write your reply to:

Draft