Node.js + Angular:Cors 问题

编程入门 行业动态 更新时间:2024-10-07 20:27:06

Node.<a href=https://www.elefans.com/category/jswz/34/1771451.html style=js + Angular:Cors 问题"/>

Node.js + Angular:Cors 问题

我需要帮助解决“fly.io”平台上的部署问题。前端和后端之间的通信可以正常运行一段时间,但随后出现此错误:

“从来源‘’访问‘’的 XMLHttpRequest 已被 CORS 策略阻止:无‘Access-Control-Allow-Origin ' 标头存在于所请求的资源上。”

问题是我没有将标头从前端发送到后端,该标头之前正在发送但突然停止了。在我将它转移到这个平台之前,我的应用程序工作正常。请问别人是怎么解决这个问题的? 我的后端配置如下:

app.use(
  cors({
    origin: [
      "",
    ],
    credentials: true,
  })
);

后台日志:

这是我的前端配置:

httpOptions = {
    headers: new HttpHeaders({
      'Content-Type': 'application/json',
      'Access-Control-Allow-Origin': '*',
    }),
    responseType: 'text' as 'json',
    withCredentials: true,
  };

fly.toml 前端:

app = "todo-front"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]

[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 8080
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

fly.toml 后端:

app = "todo-back"
kill_signal = "SIGINT"
kill_timeout = 5
processes = []

[env]
  ALLOWED_ORIGINS = ""
[experimental]
  allowed_public_ports = []
  auto_rollback = true

[[services]]
  http_checks = []
  internal_port = 3000
  processes = ["app"]
  protocol = "tcp"
  script_checks = []
  [services.concurrency]
    hard_limit = 25
    soft_limit = 20
    type = "connections"

  [[services.ports]]
    force_https = true
    handlers = ["http"]
    port = 80

  [[services.ports]]
    handlers = ["tls", "http"]
    port = 443

  [[services.tcp_checks]]
    grace_period = "1s"
    interval = "15s"
    restart_limit = 0
    timeout = "2s"

提前谢谢你

我试图将 'Access-Control-Allow-Origin': '' 更改为 'Access-Control-Allow-Origin': '*' 等没有任何作用。是托管的问题吗?

回答如下:

更多推荐

Node.js + Angular:Cors 问题

本文发布于:2024-05-30 06:43:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:js   Node   Cors   Angular

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!