javascript 印前检查具有无效的HTTP状态代码404 Jquery AJAX POST

nhaq1z21  于 2023-01-19  发布在  Java
关注(0)|答案(2)|浏览(122)

好吧,我真的很讨厌它在控制台中出现这个错误。我知道 * 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);
    }
});

这是控制台中的错误:

控制台网络显示:

加载资源失败:服务器以404(未找到)状态响应index.html:1 XMLHttpRequest无法加载http://dev-blahblah/newWS/PostData。预检响应具有无效的HTTP状态代码404
现在这是同样的请求,但在** Postman :第一节第二节第一节第三节第一节第四节第一节第五节第一节
我已经花了
天**试图弄清楚这一点,并无休止的谷歌寻找例子,我有,但似乎所有的例子都不工作。
我会非常感谢有人让我知道我需要做什么,以获得与JQUERY AJAX的工作。

  • 在CHROME = WORKS中的同一个域上运行
  • 在CHROME中的不同域上运行它=不起作用
  • 在IE = WORKS中的同一个域上运行
  • 在IE = WORKS中的不同域上运行
2guxujil

2guxujil1#

在我的API文件中使用了以下配置节来避免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>
58wvjzkj

58wvjzkj2#

删除WebDAV对我来说不起作用,因为我的服务器上可能有全局策略。但是,这将其删除:

<system.webServer>
    <security>
        <requestFiltering>
            <verbs applyToWebDAV="false">
                <add verb="DELETE" allowed="true" />
                <add verb="PUT" allowed="true" />
                <add verb="OPTIONS" allowed="true" />
            </verbs>
        </requestFiltering>
    </security>
</system.webServer>

<add verb="OPTIONS" allowed="true" />带来了不同。

相关问题