【发布时间】:2015-05-26 07:10:39
【问题描述】:
我正在像这样对Delete 方法进行 api 调用 -
$http.delete('http://localhost:45467/api/v1/proddetails/' + prodId, null )
.success(function (data, status, headers, config) {
if(data.status == "200")
{
console.log('data deleted');
}
deferred.resolve(data);
})
.error(function (data, status, headers, config) {
console.log("some error occurred");
deferred.reject(data);
});
删除端点如下所示 -
[HttpDelete]
public HttpResponseMessage Delete(int productId)
当我运行它并查看 Chrome 控制台时,我看到了这个错误 -
OPTIONS http://localhost:45467/api/v1/proddetails/34
(index):1 XMLHttpRequest cannot load
http://localhost:45467/api/v1/proddetails/34. No 'Access-Control-Allow-
Origin' header is present on the requested resource. Origin
'http://localhost:5100' is therefore not allowed access. The response had
HTTP status code 405.
似乎无法弄清楚这里出了什么问题。其他 Get 和 Post 请求工作正常。如何解决这个问题?
注意1:我已经启用了CORS -
[EnableCors(origins: "*", headers: "*", methods: "*")]
注意2:我正在使用interceptors 为每个请求添加一个身份验证令牌。我不确定这是否会导致任何问题。
注意 3:这是我在 asp.net webapiconfig 文件中定义路由的方式 -
config.Routes.MapHttpRoute(
name: "ProdApi",
routeTemplate: "api/v1/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
提前致谢。
【问题讨论】:
-
看起来像服务器端的问题stackoverflow.com/a/15619435/2435473
-
我加了,还是没用。
-
我不确定发生了什么。如果有人需要任何信息来挖掘这个,我可以提供。但是这个问题正在奇怪的程度。
-
您是否尝试从本地 chrome 扩展邮递员中删除记录?
-
我现在试了,它说-请求的资源不支持http方法'DELETE'
标签: angularjs asp.net-mvc asp.net-web-api xmlhttprequest