[英]Docker: Access to XMLHttpRequest has been blocked by CORS policy
I created an ASP.NET Web Application (.NET Framework) project in Vsual Studio 2022, and created a web service in it.我在 Vsual Studio 2022 中创建了一个 ASP.NET Web Application (.NET Framework) 项目,并在其中创建了一个 web 服务。 Everything works if the call to the web service takes place in Locall IIS.
如果对 web 服务的调用发生在 Locall IIS 中,则一切正常。 When I put the projects in the container, nothing works.(Windows containers) What am I doing wrong?
当我将项目放入容器中时,没有任何效果。(Windows 容器)我做错了什么? The following error appears: Access to XMLHttpRequest at 'http://localhost:5002/WebService.asmx/HelloWorld' from origin 'http://172.17.78.68' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
出现以下错误:从源“http://172.17.78.68”访问“http://localhost:5002/WebService.asmx/HelloWorld”处的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”header。 Here is my docker-compose.yml:
这是我的 docker-compose.yml:
version: '3.4'
services:
saview:
image: ${DOCKER_REGISTRY-}saview
build:
context: .\SAview
dockerfile: Dockerfile
ports:
- 5001:80
links:
- saviewweb
depends_on:
- "saviewweb"
networks:
- mynetwork
saviewweb:
image: ${DOCKER_REGISTRY-}saviewweb
build:
context: .\SaviewWeb
dockerfile: Dockerfile
ports:
- 5002:80
networks:
- mynetwork
networks:
mynetwork:
driver: nat
This is how I make a request with javascript:这就是我向 javascript 发出请求的方式:
function Web(arg, url ) {
var result;
$.ajax(
{
type: 'POST', url: url, data: JSON.stringify(arg),
dataType: 'json',
contentType: "application/json; charset=utf-8", async: false, success: function (res) {
result = res;
}
, error: function (a1, a2, a3) {
result =
{
d: "_Error_" + a1 + " " + a2 + " " + a3
};
} //-
});
if (result.d == null)
return null;
if (result.d.indexOf != undefined && result.d.indexOf("_Error_") !== -1) {
alert(result.d);
return null;
}
return result;
}
Web({}, "http://localhost:5002/WebService.asmx/HelloWorld" );
CORS problems originate when you attempt to request a domain different than the one JavaScript is running at.当您尝试请求与 JavaScript 正在运行的域不同的域时,CORS 就会出现问题。 Eg
http://localhost:3000
and http://example.com
.例如
http://localhost:3000
和http://example.com
。 And is fixed in the server/API rather than in the front-end.并且固定在服务器/API 中,而不是前端。
If they do share the domain everything would be fine.如果他们确实共享域,一切都会好起来的。
Please reference these docs.请参考这些文档。 Everything is in there https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0
一切都在那里https://docs.microsoft.com/en-us/aspnet/core/security/cors?view=aspnetcore-6.0
As mentioned in the docs adding something of the sorts to your Program.cs should do the trick:正如文档中提到的那样,在 Program.cs 中添加一些东西应该可以解决问题:
builder.Services.AddCors(options =>
{
options.AddPolicy(
name: "cors-policy",
policy =>
{
policy.WithOrigins(
"http://172.17.78.68:<port>",
"http://localhost:5002"
);
}
);
});
...
app.UseCors("cors-policy");
Where in WithOrigins
you would want to specify all the "front-ends" that you want to be allowed to request your API.在
WithOrigins
中,您希望指定允许请求 API 的所有“前端”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.