CORS golang echo 框架
CORS golang echo framework
我有下一个 Go 代码:
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.POST("/createuser", addUser)
e.Logger.Fatal(e.Start(":4000"))
}
和 Vue js 获取脚本
const res = await fetch('https://localhost:4000/createuser', {
method: 'POST',
headers: {
'Content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(nameAndPhone),
})
执行时我的 Firefox 控制台出错
Extraneous request blocked: Single source policy denies reading remote resource at https://localhost:4000/createuser (Reason: Failed to complete CORS request).
Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.
我的网络选项卡:
Blocked OPTIONS localhost:4000 createuser fetch CORS failed 0byte
Blocked POST localhost:4000 createuser fetch NS_ERROR_DOM_BAD_URI 0byte
它在 Postman 中有效,但由于 CORS 而无法在浏览器中使用。如果我的代码中已有 e.Use(middleware.CORS())
,为什么它不起作用?
请看下一个例子。
e := echo.New()
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
问题是我是从 https
而不是 http
修复了 js:
const res = await fetch('http://localhost:4000/createdebter', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
},
body: JSON.stringify(nameAndPhone),
})
我有下一个 Go 代码:
package main
import (
"database/sql"
"encoding/json"
"fmt"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
func main() {
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.CORS())
e.POST("/createuser", addUser)
e.Logger.Fatal(e.Start(":4000"))
}
和 Vue js 获取脚本
const res = await fetch('https://localhost:4000/createuser', {
method: 'POST',
headers: {
'Content-type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
body: JSON.stringify(nameAndPhone),
})
执行时我的 Firefox 控制台出错
Extraneous request blocked: Single source policy denies reading remote resource at https://localhost:4000/createuser (Reason: Failed to complete CORS request).
Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.
我的网络选项卡:
Blocked OPTIONS localhost:4000 createuser fetch CORS failed 0byte
Blocked POST localhost:4000 createuser fetch NS_ERROR_DOM_BAD_URI 0byte
它在 Postman 中有效,但由于 CORS 而无法在浏览器中使用。如果我的代码中已有 e.Use(middleware.CORS())
,为什么它不起作用?
请看下一个例子。
e := echo.New()
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowHeaders: []string{echo.HeaderOrigin, echo.HeaderContentType, echo.HeaderAccept},
}))
问题是我是从 https
而不是 http
修复了 js:
const res = await fetch('http://localhost:4000/createdebter', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
},
body: JSON.stringify(nameAndPhone),
})