gpt4 book ai didi

javascript - 预检具有无效的 HTTP 状态代码 404 Jquery AJAX POST

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:56 26 4
gpt4 key购买 nike

好吧,当它在控制台中出现此错误时,我真的很讨厌它。而且我知道 stackoverflow 充斥着这些类型的问题。但是,我已经完成研究并且在我的 Web API 2 Web 服务中启用了 CORS,但我仍然收到此错误。

这是我的 Web API 2 代码:

namespace WebApi.App.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
public class ServiceController : ApiController
{
[HttpGet]
[Route("GetData")]
public IHttpActionResult GetEmpData(DATAvars theDATA)
{
return Ok("WORKED! " + theDATA);
}

[HttpPost]
[Route("PostData")]
public IHttpActionResult PostEmpData(DATAvars theDATA)
{
return Ok("WORKED! " + theDATA.theID);
}

}

public class DATAvars
{
public string theID { get; set; }
public string empImg { get; set; }
}
}

<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<remove name="WebDAVModule"/>
</modules>
<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>
<handlers>
<remove name="WebDAV" />
<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>

namespace WebApi.App
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.MapHttpAttributeRoutes();
config.EnableCors();
}
}
}

public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}

protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin" , "*");

if (HttpContext.Current.Request.HttpMethod == "OPTIONS" )
{
//These headers are handling the "pre-flight" OPTIONS call sent by the browser
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();
}
}
}

然后是我的 AJAX 调用代码(托管在另一个域上):

$.ajax({
type: "POST",
crossDomain: true,
url: "http://dev-blahblah/newWS/PostData",
beforeSend: function (xhrObj) {
xhrObj.setRequestHeader("Content-Type", "application/json");
},
data: {
theID: "2135648792",
empImg: "false"
},
dataType: "json",
success: function (data) {
console.log(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
console.log(XMLHttpRequest);
}
});

这是控制台中的错误: enter image description here

控制台网络说: enter image description here

Failed to load resource: the server responded with a status of 404 (Not Found) index.html:1 XMLHttpRequest cannot load http://dev-blahblah/newWS/PostData. Response for preflight has invalid HTTP status code 404

现在这是相同的请求,但在 POSTMAN 中:

我花了 DAYS 试图解决这个问题,并且没完没了地通过 google 搜索来寻找示例,但似乎所有示例都不起作用。

如果有人让我知道我需要做什么才能使用 JQUERY AJAX,我将非常感谢

-Running it on the same domain in CHROME = WORKS

-Running it on a different domain in CHROME = DOES NOT WORK

-Running it on the same domain in IE = WORKS

-Running it on a different domain in IE = WORKS

最佳答案

在我的 Web API web.config 文件中使用了以下配置部分来避免 404 错误。

<handlers>
<remove name="WebDAV"/>
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<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>

关于javascript - 预检具有无效的 HTTP 状态代码 404 Jquery AJAX POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35329461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com