gpt4 book ai didi

javascript - Internet Explorer 中跨站点请求的访问控制

转载 作者:可可西里 更新时间:2023-11-01 02:51:51 25 4
gpt4 key购买 nike

我正在尝试从多个域向将处理请求的单个域发出 AJAX 调用。通过在处理服务器上设置标题,在 Firefox 和 Chrome 中启用跨域很容易:

header("Access-Control-Allow-Origin: *");

但这无助于在 Internet Explorer 中启用它。当我尝试时:

httpreq.send('');

它因错误访问被拒绝而停止。

如何在 Internet Explorer 中启用它?

最佳答案

自从我第一次在 IE7 及更高版本中发布我的 CORS 解决方案以来,发生了很多变化。首先,jQuery 属性 $.support.cors 默认为 true,.NET Frameworks 4.0 及更高版本不再支持 CORS,如 3.5.1 及更低版本中所实现的那样。在使用 ASP.NET 4.0 编写 Web 服务时,我安装了 Thinktecture.IdentityModel,它可以作为 NuGet 包使用。然后,在Global.asax文件的Application_Start方法中,添加:

void Application_Start(object sender, EventArgs e) 
{
Thinktecture.IdentityModel.Http.Cors.IIS.UrlBasedCorsConfiguration.Configuration
.ForResources("*")
.ForOrigins("http://localhost:80, http://mydomain")
.AllowAll()
.AllowMethods("GET", "POST");
}

现在在 system.webServer 中添加 httpProtocol 元素:

<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept, X-Requested-With, X-File-Name" />
<add name="Access-Control-Allow-Methods" value="GET, POST" />
</customHeaders>
</httpProtocol>

我的 $.ajax 调用现在是:

$.ajax({
url: serviceUrl,
data: JSON.stringify(postData),
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
error: onError
});

关于javascript - Internet Explorer 中跨站点请求的访问控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2044684/

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