Site Overlay

Go Echo 跨域(CORS)问题

国内网上的方法太繁琐。我不想啰嗦。只要两步:

  1. 引入包 "github.com/labstack/echo/middleware"

  2. 添加如下语句:

    e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
        AllowOrigins: []string{"*"},
        AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
    }))

总览:

package main

import (
    "memori/route"
    "net/http"

    "github.com/labstack/echo/middleware"
)

func main() {
    e := route.Init()
    e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
        AllowOrigins: []string{"*"},
        AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
    }))
    e.Logger.Fatal(e.Start(":1323"))
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注