Đã có nhiều thay đổi kể từ lần đầu tiên tôi gửi giải pháp của tôi cho CORS trong IE7 trở lên. Đầu tiên của thuộc tính jQuery $ .support.cors là true theo mặc định,.NET framework 4.0 trở lên không còn hỗ trợ CORS như được thực hiện trong các phiên bản 3.5.1 và thấp hơn. Khi viết một dịch vụ web với ASP.NET 4.0 tôi đã cài đặt Thinktecture.IdentityModel có sẵn như là một gói NuGet. Sau đó, trong phương pháp Application_Start của tập tin Global.asax, thêm:
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");
}
Bây giờ thêm các yếu tố httpProtocol trong system.webServer:
<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>
My $ .ajax gọi bây giờ là:
$.ajax({
url: serviceUrl,
data: JSON.stringify(postData),
type: "POST",
cache: false,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: onSuccess,
error: onError
});
Nguồn
2012-06-13 15:46:08
IE 10 hoạt động tốt, IE8/9 với giới hạn. Xem http://caniuse.com/#search=cors – gavenkoa
Những giới hạn cụ thể của hỗ trợ CORS trong IE9 là gì? – Umopepisdn