方法Delete不被访问控制允许的方法允许误差

方法Delete不被访问控制允许的方法允许误差

问题描述:

我已经开发利用angularjs当我尝试写一个删除使用$资源就会给我下面的错误前端到RESTAPI

i have developed a front end to a RESTAPI using angularjs when i try to write a DELETE using $resource it will give me following error

方法删除不被访问控制允许的方法允许的。

Method DELETE is not allowed by Access-Control-Allow-Methods.

我也做与棱角分明的东西,但我希望这可以帮助你。这里是一个解决方案:
首先在你的API web.config文件,更新

I am also doing the something with angular but I hope this may help you. Here is a solution: First have to update in your API web.config file

<clear />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
<add name="Access-Control-Allow-Methods" value="OPTIONS,GET,POST,PUT,DELETE" />

In `ApiController`

[HttpDelete]
public string Delete() 
{ 
return "u call delete"; 
} 

public HttpResponseMessage Options() { 
     var response = new HttpResponseMessage(); 
     response.StatusCode = HttpStatusCode.OK; 
return response; 
}


In Angular...

        home.post().then(function (data) {
            console.log(data);
        });

        home.remove().then(function (data) {
            console.log(data);
        });

Out Put...

> u call delete

Hope this will help you.. :)