xmlhttprequest无法加载响应URL。对于预检,$ http.delete的http状态代码为405

时间:2016-02-05 12:50:04

标签: javascript angularjs angularjs-service

我有网络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扩展程序)使用它时。我正在传递数据作为表格数据&#39;。我得到200返回代码。 我用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中设置了标题:

<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无法加载预检的响应网址无效http状态代码405

1 个答案:

答案 0 :(得分:0)

似乎正在发生的事情是服务器没有/错误地处理CORS'预检'请求(使用OPTIONS方法)。

建议阅读:

preflight请求的响应应包含类似

的内容
OPTIONS / HTTP/1.1
Origin: http://example.com
Access-Control-Request-Method: DELETE

Access-Control-Request-Method是这里的主要焦点)

至于为什么“邮差”扩展不会给这个唠叨,它根本不受Same Origin Policy

的影响