2016-03-28 81 views
17

我已徹底搜索,但在特定情況下無法找到解決此問題的解決方案。CORS已啓用,但預檢的響應在POST JSON時有無效的HTTP狀態代碼404

使用Fiddler(POST)的跨域服務調用正確執行並接收數據。但是,通過瀏覽器(Chrome),我收到消息'預檢具有無效的HTTP狀態代碼404'

我有一個Web API應用程序並已安裝CORS並確保web.config文件中存在以下內容:

<system.webServer> 
    <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" /> 
    </handlers> 
    <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> 
</system.webServer> 

這裏是Ajax調用:

var secretKey = 'difusod7899sdfiertwe08wepifdfsodifyosey', 
    url = 'http://api.intrinsic.co.uk/api/v1/PTS/ActiveDrivers?api_key=098werolllfWnCbPGAuIXVOJidDHRfYcgxImMlxTXopuekXrSOqOWzEAIdeNTWGPQPpyHxgVGsFysGFKPzq'; 

    jQuery.ajax ({ 
     url: url, 
     type: "POST", 
     data: JSON.stringify({ secretKey: secretKey}), 
     dataType: "json", 
     contentType: "application/json; charset=utf-8", 
     success: function(data){ 
      var content = "<table class=\"container\"><thead><tr><th>Driver Number</th><th>Timestamp</th><th>VRN</th><th>Latitude</th><th>Longitude</th><th>Track Link</th></tr></thead><tbody>"; 
      $.each(data.ActiveDrivers.DriverLocationStatus, function (index, element) { 
       content += "<tr><td>" + element.DriverNumber + "</td>"; 
       content += "<td>" + dateFormat(element.Timestamp, "d/m/yy") + " " + dateFormat(element.Timestamp, "h:MM TT") + "</td>"; 
       content += "<td>" + element.VRN + "</td>"; 
       content += "<td>" + element.CurrentLatitude + "</td>"; 
       content += "<td>" + element.CurrentLongitude + "</td>"; 
       content += "<td><a href=\"https://www.google.co.uk/maps/place//@" + element.CurrentLatitude + "," + element.CurrentLongitude + ",15z/\" target='_blank'>Track &raquo;</a></td></tr>"; 
      }); 
      content += "</tbody></table>"; 
      $("#result").html(content); 
     } 
    }); 

顯然,工作在完美的同一個域,如前所述,它的工作原理使用招。

我確定它是瀏覽器的預檢選項檢查失敗的內容類型的'應用程序/ json',但我不知道如何解決它。

我應該添加的web.config文件中是否缺少某些東西?

我試着刪除'內容類型'沒有任何影響。

我曾希望this article會解決這個問題(它似乎有希望的),但遇到了同樣的錯誤:

XMLHttpRequest cannot load [URL]. Response for preflight has invalid HTTP status code 404 

回答

14

我終於得到了這個工作。

這篇文章'WebAPI with CORS – IIS Intercepts OPTIONS Verb'告訴我的想法。一個圖像顯示了在IIS中,出現OPTIONS處理程序映射的位置,以及爲什麼在web.config中我們需要刪除它以確保IIS不攔截。

當我看看IIS的處理程序WAS不存在。然後我看了一下鏈接的文章'Can't set HttpHandler order using Web.Config unless a «clear» tag exists',並看到在本文中,刪除OPTION處理程序後,它被明確添加到web.config中。

因爲我在IIS中看不到OPTION處理程序,所以我也將它添加到了web.config文件中,並且都突然生效。看起來這個增加是需要的。

最終的web.config處理程序部分如下所示(注意,我決定保留最初的'刪除'以防萬一在將來我遷移到其他Web服務器時導致問題)。

<system.webServer> 
    <handlers> 
     <remove name="WebDAV"/> 
     <remove name="OPTIONSVerbHandler"/> 
     <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" /> 
     <add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="ProtocolSupportModule" requireAccess="None" responseBufferLimit="4194304" /> 
    </handlers> 
    <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, OPTIONS" /> 
     </customHeaders> 
    </httpProtocol> 
</system.webServer> 
+0

它也適用於我.. –

+0

能否請你幫我在這方面努力:https://magento.stackexchange.com/questions/170342/ magento-htaccess-response-for-preflight-has-invalid-http-status-code-400 –

+0

謝謝工作。添加處理程序也很重要。 – Dileep

34

感謝但得到405錯誤,在上面的配置更改後。

最後,它工作在網絡API添加下面的代碼後Global.asax文件

protected void Application_BeginRequest(Object sender, EventArgs e) 
    { 
     //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
     if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
     { 
      HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
      HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
      HttpContext.Current.Response.End(); 
     } 
    } 
+1

爲什麼這不是一個答案呢? – Immortal

2

我有一個類似的設置這是顯示404錯誤和500錯誤,我試圖讓我的Web服務運行CORS 。我的修補程序基本上使用了Hussain的解決方案,但在清理修復程序時,我注意到只需要一個響應行,並且能夠將原始web處理程序保留在web.config中,並且不需要移動所有的響應處理程序轉換爲代碼。

基本上,我的修補程序包括在我的ApplicationOnBeginRequest處理這個一個主要FIX

private void ApplicationOnBeginRequest(object sender, EventArgs eventArgs) 
     { 
... 
      if (context.Request.HttpMethod == "OPTIONS") 
       response.End(); 
     } 

和我的web.config這些處理程序:

<system.webServer> 
    <!--Other handlers/modules ...--> 
    <httpProtocol> 
     <customHeaders> 
      <clear /> 
      <add name="Access-Control-Allow-Origin" value="*" /> 
      <add name="Access-Control-Allow-Credentials" value="true" /> 
      <add name="Access-Control-Allow-Headers" value="Content-Type,Accept" /> 
      <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,DELETE,OPTIONS" /> 
     </customHeaders> 
    </httpProtocol> 
    </system.webServer> 

抱歉,我不能發送此請注意Hussain的回答。

9

這對我有效。

在Global.asax中

protected void Application_BeginRequest(Object sender, EventArgs e) 
{ 
    //HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
    if (HttpContext.Current.Request.HttpMethod == "OPTIONS") 
    { 
     HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept"); 
     HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000"); 
     HttpContext.Current.Response.End(); 
    } 
} 

web.config中

<httpProtocol> 
     <customHeaders> 

    <add name="Access-Control-Allow-Origin" value="*"/> 
    <add name="Access-Control-Allow-Methods" value="GET,PUT,POST,DELETE,OPTIONS"/> 
    <add name="Access-Control-Allow-Headers" value="Content-Type"/> 
     </customHeaders> 
    </httpProtocol> 

重建和變戲法。

0

對於asp核心,在Configure過程的Startup.cs中使用此代碼。我用2.0版本,但我認爲它應該與舊太

app.UseCors(builder => { 
       builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader(); 
      }); 
相關問題