Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
856 views
in Technique[技术] by (71.8m points)

GIN框架"No 'Access-Control-Allow-Origin' header is present 错误

最近用GolangGIN框架+Vue写了一个前后端分离的博客.
Go的第三方包cors创建了一个中间件,代码如下:

package middleware

import (
    "time"
    "github.com/gin-contrib/cors"
    "github.com/gin-gonic/gin"
)

// 跨域
func Cors() gin.HandlerFunc {
    return cors.New(
        cors.Config{
            AllowAllOrigins:  true,
            AllowMethods:     []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
            AllowHeaders:     []string{"*"},
            ExposeHeaders:    []string{"Content-Length", "text/plain", "Authorization", "Content-Type"},
            AllowCredentials: true,
            MaxAge:           12 * time.Hour,
        },
    )
}

image
可以看到后端已经返回了数据给浏览器。但浏览器因为Cors协议阻止数据在页面显示
image.png

这是我从码云上找到的,一摸一样的写法
ginbloger

登录的页面没有报这个跨域错误,查了很多的资料也找不到具体原因。而且我看其他人也是这样写的,他们运行就可以。想不明白,求指教。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

Please log in or register to answer this question.

1 Answer

0 votes
by (71.8m points)

cors要配置在启动9090端口的那个服务上。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to JiKe DevOps Community for programmer and developer-Open, Learning and Share

755k questions

754k answers

5 comments

53.3k users

...