xmlhttprequest 无法加载响应 URL。对于预检,$http.delete 的 http 状态代码 405 无效
xmlhttprequest cannot load response URL. for preflight has invalid http status code 405 for $http.delete
我有网络 API 服务:
public void Delete(int id)
{
}
Web api-Web.config
<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="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<validation validateIntegratedModeConfiguration="false" />
<directoryBrowse enabled="true" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
当我使用 Postman(Chrome 扩展名)使用它时。我将数据作为 'form data' 传递。我得到 200 return 代码。
我已经使用 angularjs:
调用了这个方法
deletesurvey: function(surveyData) {
var objFormData = new FormData();
for (var key in surveyData)
objFormData.append(key, surveyData[key]);
return $http.delete(configurationurl + 'Survey', objFormData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
});
我在 Web.Config 中设置了 headers:
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,HEAD,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
<add name="Access-Control-Request-Methods" value="*" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
调用 deletesurvey 函数后出现错误(浏览器是 Chrome)
xmlhttprequest 无法加载响应 URL 预检具有无效的 http 状态代码 405
似乎正在发生的事情是 CORS 'preflight' 请求(使用 OPTIONS
方法)由服务器 not/incorrectly 处理。
推荐阅读:
对 preflight
请求的响应应包含类似
的内容
OPTIONS / HTTP/1.1
Origin: http://example.com
Access-Control-Request-Method: DELETE
(Access-Control-Request-Method
是这里的主要焦点)
至于为什么'Postman'后缀没有这个nag,根本就是不受Same Origin Policy
的影响
我有网络 API 服务:
public void Delete(int id)
{
}
Web api-Web.config
<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="GET, POST, PUT, DELETE, OPTIONS" />
</customHeaders>
</httpProtocol>
<validation validateIntegratedModeConfiguration="false" />
<directoryBrowse enabled="true" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
当我使用 Postman(Chrome 扩展名)使用它时。我将数据作为 'form data' 传递。我得到 200 return 代码。 我已经使用 angularjs:
调用了这个方法deletesurvey: function(surveyData) {
var objFormData = new FormData();
for (var key in surveyData)
objFormData.append(key, surveyData[key]);
return $http.delete(configurationurl + 'Survey', objFormData, {
transformRequest: angular.identity,
headers: { 'Content-Type': undefined }
});
我在 Web.Config 中设置了 headers:
<modules runAllManagedModulesForAllRequests="true" />
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="GET,POST,PUT,HEAD,DELETE,OPTIONS" />
<add name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept" />
<add name="Access-Control-Request-Methods" value="*" />
<add name="Access-Control-Max-Age" value="1728000" />
</customHeaders>
</httpProtocol>
调用 deletesurvey 函数后出现错误(浏览器是 Chrome) xmlhttprequest 无法加载响应 URL 预检具有无效的 http 状态代码 405
似乎正在发生的事情是 CORS 'preflight' 请求(使用 OPTIONS
方法)由服务器 not/incorrectly 处理。
推荐阅读:
对 preflight
请求的响应应包含类似
OPTIONS / HTTP/1.1
Origin: http://example.com
Access-Control-Request-Method: DELETE
(Access-Control-Request-Method
是这里的主要焦点)
至于为什么'Postman'后缀没有这个nag,根本就是不受Same Origin Policy
的影响