Skip to content

CORS 跨域中间件无法执行 #23

@xmx

Description

@xmx

程序

package main

import (
	"net/http"

	"github.com/xgfone/ship/v5"
	"github.com/xgfone/ship/v5/middleware"
)

func main() {
	cors := middleware.CORS(&middleware.CORSConfig{
		AllowOrigins: []string{"*"},
		AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
		AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
	})
	r := ship.Default()
	r.Use(cors)
	r.Route("/ping").GET(func(c *ship.Context) error {
		return c.Text(http.StatusOK, "pong")
	})

	srv := &http.Server{
		Addr:    ":8080",
		Handler: r,
	}
	srv.ListenAndServe()
}

复现

$ curl -i -XOPTIONS http://127.0.0.1:8080/balabala -H "Origin: http://example.com" -H "Access-Control-Request-Method: GET"
HTTP/1.1 404 Not Found
Content-Type: text/plain; charset=UTF-8
Date: Wed, 23 Jul 2025 10:36:51 GMT
Content-Length: 9

Not Found

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions