golang 中允许使用 cors,代码如下
func register() *echo.echo { e := echo.new() e.get("/swagger/*", echoswagger.wraphandler) // swagger 등록 validator.registervalidator(e) // 유효성검사 등록 e.use(middleware.cors()) // cors 등록 ...... package middleware import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) func cors() echo.middlewarefunc { return middleware.corswithconfig(middleware.corsconfig{ alloworigins: []string{"http://localhost:3000"}, allowmethods: []string{echo.get, echo.put, echo.post, echo.delete}, allowheaders: []string{echo.headerorigin, echo.headercontenttype, echo.headeraccept}, exposeheaders: []string{echo.headercontentlength}, }) }
但是,即使我在react项目中从(localhost:3000)发送请求,cors错误也不会停止。
func (serverInfo *ServerInfo) ServerStart() { var databaseSet database.DatabaseInterface = &mysql.MysqlInfo{} // Database databaseSet.InitDatabase() // 데이터베이스를 초기화합니다 hosts := map[string]*Host{} // Hosts // API hosts[fmt.Sprintf("%v.%v:%v", "api.v1", os.Getenv(envkey.SERVER_URL), os.Getenv(envkey.PORT))] = &Host{apiV1Route.Register()} // Server e := echo.New() e.Use(middleware.Logger()) // 로거등록 e.Any("/*", func(c echo.Context) (err error) { req := c.Request() res := c.Response() host := hosts[req.Host] if host == nil { err = echo.ErrNotFound } else { host.Echo.ServeHTTP(res, req) } return }) // e.Logger.Fatal(e.StartTLS(fmt.Sprintf(":%v", os.Getenv(envkey.PORT)), "housewhale.com+3.pem", "housewhale.com+3-key.pem")) e.Logger.Fatal(e.Start(fmt.Sprintf(":%v", os.Getenv(envkey.PORT)))) }
这个问题我已经困扰了好几天了,如何解决?我已经尝试了在手册或谷歌中找到的所有内容,但 cors 错误并没有消失。
正确答案
有一个错误,没有附加错误,我附加了如下错误,当时问题就解决了。
access to fetch at 'https://api.v1.localhsot:8081/auth/retoken' from origin 'http://localhost:3000' has been blocked by cors policy: response to preflight request doesn't pass access control check: 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.
服务器端未正确允许cors,请配置服务器端以匹配请求
例如,如果客户端向标头发送授权数据,则服务器也允许在标头中进行授权。像这样设置必要的请求
package middleware import ( "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) func Cors() echo.MiddlewareFunc { return middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{"http://localhost:3000"}, AllowMethods: []string{echo.GET, echo.PUT, echo.POST, echo.DELETE}, AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept, echo.HeaderAuthorization}, ExposeHeaders: []string{echo.HeaderContentLength, echo.HeaderSetCookie}, AllowCredentials: true, // Add this line to allow credentials }) }
以上是为什么 cors 错误没有得到解决?的详细内容。更多信息请关注PHP中文网其他相关文章!

Go语言的核心特性包括垃圾回收、静态链接和并发支持。1.Go语言的并发模型通过goroutine和channel实现高效并发编程。2.接口和多态性通过实现接口方法,使得不同类型可以统一处理。3.基本用法展示了函数定义和调用的高效性。4.高级用法中,切片提供了动态调整大小的强大功能。5.常见错误如竞态条件可以通过gotest-race检测并解决。6.性能优化通过sync.Pool重用对象,减少垃圾回收压力。

Go语言在构建高效且可扩展的系统中表现出色,其优势包括:1.高性能:编译成机器码,运行速度快;2.并发编程:通过goroutines和channels简化多任务处理;3.简洁性:语法简洁,降低学习和维护成本;4.跨平台:支持跨平台编译,方便部署。

关于SQL查询结果排序的疑惑学习SQL的过程中,常常会遇到一些令人困惑的问题。最近,笔者在阅读《MICK-SQL基础�...

golang ...

Go语言中如何对比并处理三个结构体在Go语言编程中,有时需要对比两个结构体的差异,并将这些差异应用到第�...

GoLand中自定义结构体标签不显示怎么办?在使用GoLand进行Go语言开发时,很多开发者会遇到自定义结构体标签在�...


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

WebStorm Mac版
好用的JavaScript开发工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中