CORS问题与Netlify应用(API URL端点)有关。

huangapple go评论57阅读模式
英文:

CORS problem with netlify APP (API URL endpoint)

问题

I just deployed my APP in netlify and it was working fine in development, I'm using netlify functions with faunadb, and when I run locally it all works. To make it work in production I just updated the API endpoint. To run locally I was using "http://localhost:8888/api/", and in production I changed to the URL provided by netlify. In production the App doesn't work, I get a CORS error like the image below, it occurs at the login section, when authenticating the user.

Axios instance with the URL (netlify URL + "/api/", as my /.netlify/functions/:splat in netlify.toml):
Function that calls netlify function:
netlify function (return part):
error that I'm getting:

英文:

I just deployed my APP in netlify and it was working fine in development, I'm using netlify functions with faunadb, and when I run locally it all works. To make it work in production I just updated the API endpoint. To run locally I was using "http://localhost:8888/api/", and in production I changed to the URL provided by netlify. In production the App doesn't work, I get a CORS error like the image below, it occurs at the login section, when authenticating the user.

I've already tried adding the headers configuration in the response of netlify function (image) and in netlify.toml file, but didn't work either. How can I configure this properly?

Axios instance with the URL (netlify URL + "/api/", as my /.netlify/functions/:splat in netlify.toml):CORS问题与Netlify应用(API URL端点)有关。

Function that calls netlify function:
CORS问题与Netlify应用(API URL端点)有关。

netlify function (return part):
CORS问题与Netlify应用(API URL端点)有关。

error that I’m getting:
CORS问题与Netlify应用(API URL端点)有关。

答案1

得分: 0

看起来你拼错了 Access-Control-Allow-Origin

无论如何,CORS 头部应该由服务器返回,而不是由客户端发送。它应该作为对同一URL的预检 (OPTIONS) 请求的响应返回。

如果你的API正在使用ExpressJS,你可以在这里找到如何执行此操作的示例。

英文:

Looks like you misspelt Access-Control-Allow-Origin.

In any event, the CORS headers need to be returned from the server, not sent by the client. It should be returned as a response to a pre-flight (OPTIONS) request at the same URL.

You can find an example of how to do this, if your API is using ExpressJS here.

huangapple
  • 本文由 发表于 2023年2月14日 05:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定