如何使用angularJS托管的web api服务

如何使用angularJS托管的web api服务,angularjs,asp.net-web-api,Angularjs,Asp.net Web Api,您好,我已经开发了一个小型web api crud应用程序,并托管在IIS服务器中。我能做所有的手术。我托管在服务器192.168.0.213:7777上,它工作正常。我试图通过angularjs从另一个应用程序访问这些服务,如下所示 this.deleteSubscriber = function (user_id) { var url = 'http://192.168.0.213:7777/api/User_Creation/' + user_id; re

您好,我已经开发了一个小型web api crud应用程序,并托管在IIS服务器中。我能做所有的手术。我托管在服务器192.168.0.213:7777上,它工作正常。我试图通过angularjs从另一个应用程序访问这些服务,如下所示

 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;
        });
    }
每当我试图删除用户时,都会收到如下错误消息

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.

我可以知道为什么我会遇到这个问题吗?我是否需要在iis或我的应用程序中进行任何更改?谢谢…

您可以通过配置CustomHeader在iis8中启用CORS

<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>


您可以通过配置CustomHeader在iis8中启用CORS

<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>


谢谢您的留言。我可以知道哪个标题吗?我对此不确定,因为我对web api不熟悉。您的angular应用程序和web服务是否在单独的ip上运行?是的。。在单独的ip中运行。@如果我右键单击站点,我将不获取属性选项卡。我可以选择浏览、编辑权限、添加应用程序、添加虚拟目录等等。谢谢您的留言。我可以知道哪个标题吗?我对此不确定,因为我对web api不熟悉。您的angular应用程序和web服务是否在单独的ip上运行?是的。。在单独的ip中运行。@如果我右键单击站点,我将不获取属性选项卡。我可以选择浏览、编辑权限、添加应用程序、添加虚拟目录等等。谢谢。如果我给出了控制器级别,比如[EnableCors(起源:,标题:,方法:)],那么在方法中我应该提到PUT,DELETE,GET吗?在web.config中,我发现如果有这段代码就可以了吗?我编辑了配置文件并添加了控制器级别以上的代码,但仍然没有运气。例如,在控制器上面的DELETE中,我有[AcceptVerbs](“删除”)]我是否需要在此处更改任何内容?谢谢。如果我提供控制器级别,如[EnableCors(源代码:,标题:,方法:”)]然后在方法中我应该提到PUT、DELETE、GET吗?在web.config中,我发现如果这段代码在那里可以吗?我编辑了配置文件并添加了控制器级别以上的代码,但仍然没有成功。例如,在控制器上面的DELETE中,我有[AcceptVerbs(“DELETE”)]我需要在这里更改什么吗?