2016-11-29 68 views
0

做使用jQuery AJAX一个简单的POST到IIS Web服务vb.netXMLHttpRequest无法加载URL。预检响应具有无效的HTTP状态码405

<html> 
    <head> 
     <script type="text/javascript" src="./js/jquery-3.1.1.min.js"></script> 
     <script type="text/javascript" src="./js/json-2.4.min.js"></script> 
     <script type="text/javascript" src="./js/util.js"></script> 
    </head> 
    <body> 
    <script type='text/javascript'> 
     var myKeyVals = { userid : "USERID", password : "password", gkey : "key"} 
     $.ajax({ 
      url: "myurl", 
      type: "POST", 
      data: JSON.stringify(myKeyVals), 
      contentType: "application/json", 
      cache: false, 

      success: function(data, textStatus, jQxhr){ 
       alert(JSON.stringify(data)) 
      }, 
      error: function(jqXhr, textStatus, errorThrown){ 
       alert("error") 
      } 
     }); 
    </script> 

    </body> 
</html> 

这似乎与IE浏览器,但Chrome和Firefox合作扔

“ XMLHttpRequest的无法加载网址。预检响应具有无效的HTTP状态代码405" 错误

我的web.config有

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

<handlers> 
    <remove name="OPTIONSVerbHandler" /> 
    <add name="OPTIONSVerbHandler" path="*" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="bitness32" /> 
</handlers> 

根据无数其他职位。

回答

0

所以在我的情况下,我没有一个global.asax,所以我需要通过右键单击我的项目并添加一个新项目来创建一个。从那里你可以添加一个Global.asax。在Global.asax中,我必须在BeginRequest子文件中执行以下操作:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) 
     'HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
     If HttpContext.Current.Request.HttpMethod = "OPTIONS" Then 
      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() 
     End If 
    End Sub 
相关问题