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"))
}

发表留言

本站启用了垃圾评论检测插件,如果误删请联系我~