无法 POST 到 IHTTPHandler 类,Access-Control-Allow-Origin 不允许来源

Sar*_*els 5 .net ajax ssl jquery cors

我有一个 C# SOAP Web 服务以及一些IHTTPHandler部署到https://localhost:123. 我有一个 HTML 页面,http://localhost试图使用 jQuery 将 AJAX POST 请求发送到这些处理程序之一。我每次都会得到以下信息:

XMLHttpRequest 无法加载https://localhost:123/(S(the_session_id))/MyHandler.ashxhttp://localhostAccess-Control-Allow-Origin 不允许来源。

我尝试创建一个CrossOriginModule模块和处理程序,就像这个问题CrossOriginHandler一样,按照他们的建议更新我的 web.config 。我已经尝试添加到我的处理程序中,正如这个问题中所建议的。我尝试按照这篇博客文章中的步骤添加到web.config 中的/中。没有任何帮助。context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with");ProcessRequestOPTIONSSimpleHandlerFactory-Integrated-4.0system.webServerhandlers

这是我的处理程序ProcessRequest

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";
        context.Response.AppendHeader("Access-Control-Allow-Origin", "*");
        context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with");
        context.Response.Write( /* some JSON string */ );
    }
Run Code Online (Sandbox Code Playgroud)

这是我的 web.config 的相关部分:

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
    </customHeaders>
  </httpProtocol>
  <modules runAllManagedModulesForAllRequests="true">
    <add name="CrossOriginModule" preCondition="managedHandler" type="MyNamespace.CrossOriginModule, MyNamespace, Version=1.0.0.0, Culture=neutral" />
  </modules>
  <handlers>
    <add name="CrossOrigin" verb="OPTIONS" path="*" type="MyNamespace.CrossOriginHandler, MyNamespace, Version=1.0.0.0, Culture=neutral" />
    <remove name="SimpleHandlerFactory-Integrated-4.0" />
    <add name="SimpleHandlerFactory-Integrated-4.0" path="*.ashx" verb="GET,HEAD,POST,DEBUG,OPTIONS" type="System.Web.UI.SimpleHandlerFactory" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode,runtimeVersionv4.0" />
    <add name="MyHandler" verb="*" path="MyHandler.ashx" type="MyNamespace.MyHandler, MyNamespace, Version=1.0.0.0, Culture=neutral" />
  </handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

使用 jQuery 1.7.2,以下是我向处理程序发出请求的方式:

$.ajax({
    url: url,
    data: {user: userName, pass: password},
    type: 'POST',
    dataType: 'json',
    success: function(data, textStatus, jqXHR) {
        ...
    }
});
Run Code Online (Sandbox Code Playgroud)

我也没有在服务器上的会话中使用 cookie,<sessionState cookieless="true" />如果这很重要的话,我的 web.config 中也是如此。我的服务器是IIS 7.5。

我能够对大多数请求使用 JSONP,因为我没有在 URL 中传递敏感数据。但是,对于这个处理程序,我需要执行常规 POST,因为它涉及发送纯文本密码。它通过 SSL 进行,但本文建议即使通过 SSL 也不要在 URL 中传递敏感数据。因此,我需要通过 AJAX 向 https URL 发出 POST 请求并返回 JSON,并且请求页面不会与服务器位于同一主机上。

编辑:我尝试过的其他一些事情:

更改我的 web.confighttpProtocol部分:

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/>
    <add name="Access-Control-Allow-Headers" value="x-requested-with"/>
    <add name="Access-Control-Allow-Origin" value="*" /><!-- Also tried http://localhost -->
  </customHeaders>
</httpProtocol>
Run Code Online (Sandbox Code Playgroud)

我添加context.Response.AppendHeader("Access-Control-Allow-Methods", "POST");ProcessRequest. 我还尝试context.Response.AppendHeader("Access-Control-Allow-Methods", "POST,OPTIONS");将 jQueryajax选项更改为 include contentType: 'application/json',这会导致它发出 OPTIONS 请求而不是 POST ——这仍然失败。

我应该IHTTPHandler在 web.config 中列出所有自定义类吗<handlers>?我MyHandler现在已经有了它verb="*",但它似乎没有帮助。

编辑:所以还有一些奇怪的地方。根据我的日志输出,当我向服务器端发布请求时,看起来我的请求实际上正在服务器端进行,但浏览器的行为就像被拒绝一样,仍然告诉我“不允许来源本地主机”。因此,我对服务器的 AJAX POST 验证了我的身份,就像我希望的那样,但浏览器取消了请求,并且 Chrome 控制台中显示错误,因此我的success事件处理程序不会触发。

mon*_*sur 3

您的处理程序的 ProcessRequest 还应该设置以下标头:

Access-Control-Allow-Methods: POST
Run Code Online (Sandbox Code Playgroud)

(基本上,这个标头应该与 Access-Control-Allow-Headers 响应标头一起设置在 OPTIONS 响应上)。