简体   繁体   English

Docker:对 XMLHttpRequest 的访问已被 CORS 策略阻止

[英]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:3000http://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.

相关问题 CORS 策略已阻止从源“null”访问 XMLHttpRequest? - Access to XMLHttpRequest from origin 'null' has been blocked by CORS Policy? CORS 策略已阻止从源“http://...”访问“...”处的 XMLHttpRequest - Access to XMLHttpRequest at '...' from origin 'http://...' has been blocked by CORS policy CORS 策略已阻止从原点 '' 访问 XMLHttpRequest - Access to XMLHttpRequest at '' from origin '' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present 将文件上传到 DigitalOcean Spaces,得到“从源 (url) 访问 (url) 处的 XMLHttpRequest 已被 CORS 策略阻止” - Uploading file to DigitalOcean Spaces, get "Access to XMLHttpRequest at (url) from origin (url) has been blocked by CORS policy" CORS策略阻止了对来自&#39;null&#39;的&#39;http://github/..file.json&#39;的XMLHttpRequest的访问: - Access to XMLHttpRequest at 'http://github/..file.json' from origin 'null' has been blocked by CORS policy: AWS S3 js SDK:从源访问 XMLHttpRequest 已被 CORS 策略阻止 - AWS S3 js SDK: Access to XMLHttpRequest at from origin has been blocked by CORS policy 如何解决`Access to XMLHttpRequest from Origin is has been blocked by CORS policy.`错误? - How to solve `Access to XMLHttpRequest from Origin is has been blocked by CORS policy.` error? Javascript XMLHttpRequest-已被CORS策略阻止:所请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Javascript XMLHttpRequest - has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource CORS 策略已阻止从源“localhost:3000”访问“...”处的 XMLHttpRequest - Access to XMLHttpRequest at '…' from origin 'localhost:3000' has been blocked by CORS policy Javascript 中的套接字 IO 错误:“访问 XMLHttpRequest 已被 Z5A8FEFF0B4BDE3BEEC9D924 阻止” - Socket IO error in Javascript: “Access to XMLHttpRequest from origin 'null' has been blocked by CORS policy”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM