gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 12:49:00 26 4
gpt4 key购买 nike

我有一个 C# SOAP Web 服务以及一些自定义 IHTTPHandler类部署到 https://localhost:123 .我在 http://localhost 上有一个 HTML 页面尝试使用 jQuery 将 AJAX POST 请求发送到其中一个处理程序。我每次都会得到以下信息:

XMLHttpRequest cannot load https://localhost:123/(S(the_session_id))/MyHandler.ashx. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

我试过创建一个 CrossOriginModule模块和 CrossOriginHandlerthis question 中的处理程序,按照他们的建议更新我的 web.config。我试过添加 context.Response.AppendHeader("Access-Control-Allow-Headers", "x-requested-with");到我的处理程序的 ProcessRequest , 如 this question 中所建议.我尝试按照 this blog post 中的步骤操作关于添加OPTIONSSimpleHandlerFactory-Integrated-4.0system.webServer/handlers在 web.config 中。没有任何帮助。

这是我的处理程序的 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 */ );
}

这是我的 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>

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

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

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

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

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

更改我的 web.config httpProtocol部分:

<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>

我添加了 context.Response.AppendHeader("Access-Control-Allow-Methods", "POST");ProcessRequest .我也试过context.Response.AppendHeader("Access-Control-Allow-Methods", "POST,OPTIONS");并更改我的 jQuery ajax要包含的选项 contentType: 'application/json' ,这导致它发出 OPTIONS 请求而不是 POST——这仍然失败。

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

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

最佳答案

您的处理程序的 ProcessRequest 还应设置以下 header :

Access-Control-Allow-Methods: POST

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

关于jquery - 无法 POST 到 IHTTPHandler 类,Access-Control-Allow-Origin 不允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11233367/

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