【发布时间】:2014-08-12 16:50:55
【问题描述】:
我正在从我的 angular.js 客户端向 asp.net web api PUT 方法发出以下请求:
var org = {
OrgId: 111,
name: 'testing testing'
};
$http.put("http://localhost:54822/api/data/putorganisation/1/", org).then(function(status) {
console.log("success PUT");
return status.data;
});
但是得到以下错误消息(在提琴手中):
{"message":"The requested resource does not support http method 'OPTIONS'."}
这是我的 asp.net web api web.config 文件的一部分:
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type,x-xsrf-token,X-Requested-With" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<remove name="WebDAV" />
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule" />
</modules>
</system.webServer>
数据控制器 web api:
public HttpResponseMessage Options()
{
var response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
return response;
}
public HttpResponseMessage PutOrganisation(int id, [FromBody]Organisation org)
{
var opStatus = _Repository.UpdateOrganisation(org);
if (opStatus.Status)
{
return Request.CreateResponse<Organisation>(HttpStatusCode.Accepted, org);
}
return Request.CreateErrorResponse(HttpStatusCode.NotModified, opStatus.ExceptionMessage);
}
这是我的问题:为什么我在提琴手(有效)中发出与 angularclient(无效)完全相同的请求时会收到错误消息(见上文)??
【问题讨论】:
标签: angularjs rest asp.net-web-api cors