英文:
How can I release my api for anyone to consume?
问题
我有一个使用Go、Gin和libpq构建的服务器,我的前端正在尝试调用API,但是出现了错误。错误信息如下:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Access to fetch at 'https://--brapoio-t.azurewebsites.net/-/-o' from origin 'https://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
<!-- end snippet -->
(URL被更改了,目的是在这里提问)
我的项目分为以下几部分:
main.go
routes:
//routes
main.go:
package main
import (
"os"
"github.com/gin-gonic/gin"
"github.com/guilherm5/brapoio/routes"
)
func main() {
Router := gin.New()
Router.Use(gin.LoggerWithWriter(gin.DefaultErrorWriter))
//tem toda minha parte de configuração
routes.ConfiguracaoSistema(Router)
routes.ConfiguracaoModulo(Router)
routes.ModuloSistema(Router)
routes.ArvoreMenu(Router)
routes.ModuloArvoreMenu(Router)
//tem toda minha parte de Usuario
routes.Usuario(Router)
routes.EmpresaUsuario(Router)
routes.CampoTabelaUsuario(Router)
routes.ProgramaUsuario(Router)
routes.FuncionalidadeProgramaUsuario(Router)
routes.TabelaUsuario(Router)
routes.ParametroFiltroTabelaUsuario(Router)
//meu servidor de autenticação
routes.JWTAutentication(Router)
listenAddr := ":1000"
if val, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok {
listenAddr = ":" + val
}
Router.Run(listenAddr)
}
routes包的示例代码:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
package routes
import (
"github.com/gin-gonic/gin"
"github.com/guilherm5/brapoio/controllers"
)
//NAO ESQUECER DE FAZER AZURE
func ModuloSistema(c *gin.Engine) {
c.GET("/api/moduloSistema", controllers.GetModuloSistema())
c.GET("/api/moduloSistemaID", controllers.GetModuloSistemaID())
c.POST("/api/moduloSistema", controllers.NewModuloSistema())
c.PUT("/api/moduloSistema", controllers.UpdateModuloSistemao())
c.DELETE("/api/moduloSistema", controllers.DeleteModuloSistema())
}
<!-- end snippet -->
我该如何解决前端遇到的这个错误?
英文:
I have a server made in go, gin and libpq, my front end is trying to consume the api but it is giving an error..
this error:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
Access to fetch at 'https://--brapoio-t.azurewebsites.net/-/-o' from origin 'https://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
<!-- end snippet -->
(the url was changed on purpose to ask the question here on stack overflow)
My project is divided like this:
main.go
routes:
//routes
main.go:
package main
import (
"os"
"github.com/gin-gonic/gin"
"github.com/guilherm5/brapoio/routes"
)
func main() {
Router := gin.New()
Router.Use(gin.LoggerWithWriter(gin.DefaultErrorWriter))
//tem toda minha parte de configuração
routes.ConfiguracaoSistema(Router)
routes.ConfiguracaoModulo(Router)
routes.ModuloSistema(Router)
routes.ArvoreMenu(Router)
routes.ModuloArvoreMenu(Router)
//tem toda minha parte de Usuario
routes.Usuario(Router)
routes.EmpresaUsuario(Router)
routes.CampoTabelaUsuario(Router)
routes.ProgramaUsuario(Router)
routes.FuncionalidadeProgramaUsuario(Router)
routes.TabelaUsuario(Router)
routes.ParametroFiltroTabelaUsuario(Router)
//meu servidor de autenticação
routes.JWTAutentication(Router)
listenAddr := ":1000"
if val, ok := os.LookupEnv("FUNCTIONS_CUSTOMHANDLER_PORT"); ok {
listenAddr = ":" + val
}
Router.Run(listenAddr)
}
an example of package routes:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
package routes
import (
"github.com/gin-gonic/gin"
"github.com/guilherm5/brapoio/controllers"
)
//NAO ESQUECER DE FAZER AZURE
func ModuloSistema(c *gin.Engine) {
c.GET("/api/moduloSistema", controllers.GetModuloSistema())
c.GET("/api/moduloSistemaID", controllers.GetModuloSistemaID())
c.POST("/api/moduloSistema", controllers.NewModuloSistema())
c.PUT("/api/moduloSistema", controllers.UpdateModuloSistemao())
c.DELETE("/api/moduloSistema", controllers.DeleteModuloSistema())
}
<!-- end snippet -->
How can I solve this error that my front end has been getting?
答案1
得分: 2
我认为首先你应该了解CORS(跨源资源共享)的原理,以理解为什么它会阻止你的前端网站访问资源。你可以阅读Mozilla的文档以获取更详细的信息。对于你使用Gin的情况,你可以添加CORS中间件来启用它。
英文:
I think first you should read about CORS to understand why it blocks your Frontend site to access the resources. You can read this doc from Mozilla for more detail. For your case with Gin, you can add CORS middleware to enable it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论