当前位置: 首页 > 知识库问答 >
问题:

从http://localhost:1111/...http://localhost:4200访问XMLHttpRequest已被CORS策略阻止:

左丘曦
2023-03-14

交叉起源没有发生,被CORS策略阻止。

我使用的是Windows系统。

-.service.ts文件中:

 makeHeaders() {
    const headers = new HttpHeaders({
      'content':"application/json",
      'Content-Type': 'application/x-www-form-urlencoded',
      // 'Access-Control-Allow-Credentials': 'true',
      'Access-Control-Allow-Origin': 'http://localhost:4200',
      'Access-Control-Allow-Methods': 'OPTIONS, GET, POST',
      'Access-Control-Allow-Headers': 'Origin, Content-Type, Accept, Access-Control-Allow-Origin, Authorization, X-Requested-With'
      })
    return headers;
  }

proxy.conf.json文件中:

"target": "http://localhost:1111"

我在浏览器控制台中遇到此错误:

CORS策略阻止了从http://localhost:1111/api/v1/employees/createhttp://localhost:4200对XMLHttpRequest的访问:对预飞行请求的响应没有通过权限改造检查:请求的资源上没有访问控制允许起源标头。<--plhd--错误响应

谢谢

共有1个答案

丁阳炎
2023-03-14

你可以试试这个

app.options("/*", function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
    res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
    res.sendStatus(200);
});

app.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    next();
});

在使用localhost时,需要安装插件。

Chrome

https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en

火狐

https://addons.mozilla.org/en-US/firefox/addon/cors-everywhere/

谢谢

 类似资料:
  • 请求标头如下所示: 临时头显示为access-control-request-headers:content-type access-control-request-method:POST origin:http://localhost:4200 referer:http://localhost:4200/login user-agent:mozilla/5.0(Macintosh;Intel M

  • 有一个应用程序在角7也有一个. net web api解决方案,当我调用一个动作CORS问题发生,错误如下 CORS策略阻止从http://localhost:57467/UserDetails/GetUserDetailshttp://localhost:4200访问XMLHttpRequest:对预试请求的响应不通过权限改造检查:请求的资源上不存在访问控制允许起源标头。 我试图使用Web ap

  • 我有一个。NET核心应用程序,它有一个REST API被一个角客户机使用,但遇到了CORS问题。 但仍在客户端(运行在本地端口4200上)中获取错误: CORS策略阻止从来源“HTTP://localhost:4200”访问位于“HTTP://localhost/myapi/api/table”的XMLHttpRequest:对飞行前请求的响应没有通过访问控制检查:它没有HTTP ok状态。

  • Ionic 4项目HTTP请求我收到一个错误 通过“https://u****测试”访问XMLHttpRequest。f****t.co。in/getMccList“来自源”http://localhost'已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在'access control Allow Origin'标头。 我已经添加插件科尔多瓦-插件白名单

  • 当我使用HTML2Canvas评估图像时,我得到了这个错误。 CORS策略阻止从来源“http://localhost:8080”访问位于“http://testdomain.com/storage/images/products/user_front.png”的映像:请求的资源上没有“Access-Control-Allow-Origin”标头。 下面是我的cors配置。 有人能帮我吗?

  • 在解析SiteKey后,我可以从re-captcha中获得响应。 我用我的响应和密钥向google recaptcha api做了一个手动post请求,它确实返回了我需要的结果,成功状态为true/false。 接下来,我尝试执行http,但它开始返回以下错误: 我尝试向我的angular添加一个代理conf,但是它不起作用。我在PHP上做了同样的应用程序,它工作了。我能知道有什么解决办法吗? 以