Niranjan Godbole
Niranjan Godbole

Reputation: 2175

How to consume web api services hosted from angularJS

Hi I have developed one small web api crud application and hosted in IIS server. I am able to do the all the operations. I hosted in the server 192.168.0.213:7777 and it is working fine. These services i am trying to access from another application through angularjs as below.

 this.deleteSubscriber = function (user_id) {
        var url = 'http://192.168.0.213:7777/api/User_Creation/' + user_id;
        return $http.delete(url).then(function (response) {
            return response.data;
        });
    }

Whenever i tried to delete user i am getting error message as below.

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
XMLHttpRequest cannot load http://192.168.0.213:7777/api/User_Creation/37. 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:26079' is therefore not allowed access. The response had HTTP status code 405.

May i know why I am getting this issue? Do i need to make any changes in the iis or in my application? Thank you...

Upvotes: 0

Views: 207

Answers (1)

Yasser Shaikh
Yasser Shaikh

Reputation: 47794

You can enable CORS in iis8 by configuring customHeaders

<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="POST,GET,DELETE"/>
    </customHeaders>
  </httpProtocol>
</system.webServer>

Can also be configured at code level - Enabling Cross-Origin Requests in ASP.NET Web API 2

Upvotes: 4

Related Questions