CORS已启用,但预检的响应在POST JSON时有无效的HTTP状态代码404
我已彻底搜索,但在特定情况下无法找到解决此问题的解决方案。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 »</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
我终于得到了这个工作。
这篇文章'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>
感谢但得到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();
}
}
为什么这不是一个答案呢? – Immortal
我有一个类似的设置这是显示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的回答。
这对我有效。
在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>
重建和变戏法。
对于asp核心,在Configure过程的Startup.cs中使用此代码。我用2.0版本,但我认为它应该与旧太
app.UseCors(builder => {
builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
});
它也适用于我.. –
能否请你帮我在这方面努力:https://magento.stackexchange.com/questions/170342/ magento-htaccess-response-for-preflight-has-invalid-http-status-code-400 –
谢谢工作。添加处理程序也很重要。 – Dileep