微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – Preflight具有无效的HTTP状态代码404 Jquery AJAX POST

好吧,当它在控制台中出现此错误时,我真的很讨厌它.而且我知道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

@R_404_4761@ to load resource: the server responded with a status of 404 (Not Found)
index.html:1 XMLHttpRequest cannot load 07002. Response for preflight has invalid HTTP status code 404

在这是SAME请求但在POSTMAN中:




我花了DAYS试图弄清楚这个和无休止的googleing找到例子,我有,但似乎所有的例子都不起作用.

我非常感谢有人让我知道我需要做什么才能使用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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐