Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am using MVC 4 application with angular js and calling Web API hosted on another url.
Both are my localhost but port are different.

Web API:
C#
[HttpPost]
        public int SaveClient(TB_Client client)
        {
ClientManager cmObj = new ClientManager();
            return cmObj.AddModifyClient(client);
        }


Web API config file:
XML
<system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*"/>
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
</system.webSerer> 



MVC application that calling this web api:
JavaScript
$http.post('http://localhost:54212/api/Client/SaveClient', $scope.ClientObj).success(function (data, status, header, config) {
                    alert('Client added');
                }).
                error(function () {
                    alert("error");
                });



But I still getting the error:
XMLHttpRequest cannot load http://localhost:54212/api/Client/SaveClient. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:52298' is therefore not allowed access. The response had HTTP status code 405.


Please guide me where I am lacking.
Thanks :)
Posted
Updated 4-Apr-16 15:33pm
v2

Not sure what kind of setup you have, but maybe these articles can help you.

I want to add CORS support to my server[^]

HTML5 Rocks: Using CORS[^]
 
Share this answer
 
It seems you need to Enable Cross-Origin Requests in ASP.NET Web API 2.

1) If you're using Visual Studio, then you can easily install the following Nuget package through Package Manager Console
Install-Package Microsoft.AspNet.WebApi.Cors

2) Then open the file App_Start/WebApiConfig.cs. Add the following config.EnableCors(); line code to the WebApiConfig.Register method.
public static void Register(HttpConfiguration config)
     {
         // New code
         config.EnableCors(); <-------

         config.Routes.MapHttpRoute(
             name: &quot;DefaultApi&quot;,
             routeTemplate: &quot;api/{controller}/{id}&quot;,
             defaults: new { id = RouteParameter.Optional }
            );
        }

3) Finally, you can add the [EnableCors] attribute on top of your Web API 2 Controller as followed:

using System.Web.Http.Cors;

namespace YourWebAPI2App.Controllers
{
    [EnableCors("*", "*", "*")]
    public class TestController : ApiController
    {
      ....


You can check following link for more details on EnableCors options:

http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

Hope it helps

@MawashiKid
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900