- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
昨天我问了一个关于我在尝试从 Angular 应用程序向 FastApi 后端发出 POST 请求时遇到的 CORS 错误的问题。经过一些评论后,我决定删除问题以更好地重新检查所有内容。
所以事情有点奇怪。在我的 FastApi 后端中,我有以下功能:
@app.post("/hello")
def read_root(request: Request):
print(request)
client_host = request.client.host
return {"client_host": client_host}
@app.post("/pattern-data")
def pattern_input(payload: PatternReconData) -> Dict:
# Does stuff and falls over tragically
return result # this doesn't happen of course
在我的前端,我正在尝试这两种方法:
this.apiService.sendRequest('hello', 'howdy').subscribe(
(response) => {
console.log(response);
},
(error: any) => {
console.log(error);
},
() => {
console.log('done');
}
);
this.apiService.sendRequest('pattern-data', payload).subscribe(
(response: SuccessResponse) => {
console.log(response);
/* do stuff */
},
(error: any) => {
console.log(error);
},
() => {
console.log('done');
},
);
奇怪的是,如果我在 Swagger UI 中测试向 'pattern-data'
发送一些值,由于脚本失败,它会返回错误 500。 'hello'
工作得很好。
同样,如果我从前端应用程序输入 .../hello
,我会得到预期的响应:
{client_host: '<some valid ip>'}
但是当尝试将值发送到 .../pattern-data
时导致 CORS 错误:
Access to XMLHttpRequest at 'http://<my-backend-server>/pattern-data' from origin 'http://localhost:4200' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
但我知道后端已成功接收到负载,因为如果我查看后端日志,我会看到这一点。
2021-09-16 02:42:38.0130,main,DEBUG,Pattern name received from front-end {data}
所以这表明飞行前检查成功,网络应用程序被允许与后端通信,所以这一切都与 CORS 无关。
这有点令人困惑,让我很伤心,因为我花了很多时间研究 CORS 以及如何处理 CORS 错误,而实际上我的问题与它无关。
我不明白为什么我没有收到错误 500 而是 CORS 错误。这是因为后端根本无法发送响应并且浏览器 (Chrome) 将无响应解释为没有必要的 header 并显示 CORS 错误吗?
这是来自 Chrome 开发工具标题部分。戳/hello
:
Request URL: http://<my-backend-server>/hello
Request Method: POST
Status Code: 200 OK
Remote Address: <some valid ip>
Referrer Policy: strict-origin-when-cross-origin
access-control-allow-credentials: true
access-control-allow-origin: http://localhost:4200
content-length: 31
content-type: application/json
date: Thu, 16 Sep 2021 02:43:09 GMT
server: uvicorn
vary: Origin
发送数据到/pattern-data
:
Request URL: http://<my-backend-server>/pattern-data
Referrer Policy: strict-origin-when-cross-origin
content-length: 21
content-type: text/plain; charset=utf-8
date: Thu, 16 Sep 2021 02:43:09 GMT
server: uvicorn
最佳答案
发生错误时,响应中预期的 CORS header 不存在(由于发生异常,中间件不会运行)。
由于缺少预期的 CORS header ,浏览器除了返回 CORS 错误外别无选择,即使基础错误是 500 错误。如果它确实返回了更多信息,那么浏览器将泄漏不允许读取的信息(因为不存在 CORS header )。
CORS 仅保护您免受合规客户端(即适当的浏览器)的侵害,不允许第三方站点代表浏览器用户发出请求;它不会阻止任何其他人使用 curl 或类似的东西对您的服务发出随机请求(但他们无法访问可以在浏览器用户的上下文中发出请求的信息)。
关于angular - 获取 CORS 错误而不是错误 500(不存在 'Access-Control-Allow-Origin' header ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69201995/
我尝试访问此 API click here for more info POST https://api.line.me/v2/oauth/accessToken 但总是得到错误: XMLHttpRe
我在掌握 CORS 概念时遇到问题... 我认为同源策略保护应用程序不会对“不受信任的域”进行 ajax 调用。所以, mydomain.com 向 发出 ajax 调用somedomain.com
一个技术性很强的问题,可能只有了解浏览器内部结构的人才能回答...... 浏览器缓存 CORS 预检响应的准确程度如何(假设在对 OPTIONS 预检请求的响应中返回了 Access-Control-
我一直在阅读 CORS以及它是如何工作的,但我发现很多事情令人困惑。例如,有很多关于事情的细节,比如 User Joe is using browser BrowserX to get data fr
在 OWASP site 上看到这个矛盾,我感到很困惑。 CORS 备忘单: 使用 Access-Control-Allow-Credentials: true 响应 header 时要特别小心。将允
我们无法在飞行前恢复使用 cors:任何帮助都非常感谢 ==========错误========================== About to connect() to localhost p
跨域请求字体文件时,您必须确保允许请求域使用 CORS header 访问字体文件: 访问控制允许来源 访问控制允许凭据 然而,这在请求图像时不是必需的,无论是对于 img元素或 background
CORS 规范没有说明服务器应如何响应无效的 CORS 请求。例如,如果请求 Origin 无效,则 CORS spec states :“终止这组步骤。请求超出了本规范的范围。”其他错误情况也有类似
我在理解同源策略和“解决”它的不同方法时遇到了一些麻烦。 很明显,同源策略是作为一种安全措施而存在的,因此来自服务器/域的一个脚本无法访问来自另一个服务器/域的数据。 也很明显,有时能够打破此规则很有
我正在尝试使用 cloudformation 在 API Gateway 中部署 API。这些方法需要启用 CORS,我遵循此处的模板 Enable CORS for API Gateway in C
我正在构建一个使用 CORS 的 REST 应用程序。每个 REST 调用都是不同的,我发现获取预检 OPTIONS 调用会产生很大的开销。有没有办法缓存并应用预检选项结果,以便对同一域的任何后续调用
我正在将我的 WebApi 升级到 MVC6。 在 WebApi 中,我可以拦截每个 HTTP 请求,如果是预检,我可以用浏览器可以接受的 header 进行响应。 我正在尝试找出如何在 MVC6 W
假设一个 CORS 预检请求进来了,但它为一个或多个 Access-Control-Request-* 指定了一个不受支持的值。标题。服务器应该如何将其传达回浏览器? 一些例子: 浏览器发送带有 Ac
问题中的一切。 附加信息: 使用 Win 10,GraphDB 免费,9.1.1 • RDF4J 3.0.1 • Connectors 12.0.2 我在控制台 => 设置中添加了 graphdb.w
我正在尝试通过 jQuery 调用 Sonar 网络服务之一。由于调用是跨域进行的,因此调用在 Chrome 上失败并出现以下错误: 请求的资源上不存在“Access-Control-Allow-Or
我想使用 NestJs api,但我在每个 fetch 中都收到相同的错误消息: Access to fetch at 'http://localhost:3000/articles' from or
我不确定这是否属于这里,但我在开发我的 svelte 应用程序时遇到了问题。 在开发过程中,它目前在独立服务器上运行(遵循使用 rollup 和 sirv 的指南)并针对不同端口上的后端 API。 稍
如果在服务器上正确设置 CORS 以仅允许某些来源访问服务器,这是否足以防止 XSRF 攻击? 最佳答案 更具体地说,很容易错误地认为如果 evil.com 由于 CORS 无法向 good.com
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我在 Istio 入口上启用 CORS 时遇到问题。正如 Istio Ingress 文档所述,“ingresskubernetes.io”注释将被忽略。是否可以在 Istio 入口上启用 CORS?
我是一名优秀的程序员,十分优秀!