PHP小编小新今天为大家介绍的是一个常见的网络开发问题:无法从主域访问子域,出现了“Access-Control-Allow-Origin”错误。这个问题在前端开发中经常遇到,尤其在跨
PHP小编小新今天为大家介绍的是一个常见的网络开发问题:无法从主域访问子域,出现了“Access-Control-Allow-Origin”错误。这个问题在前端开发中经常遇到,尤其在跨域请求时较为常见。它通常会导致请求被浏览器拦截,从而无法正常获取所需的数据。在本文中,我们将详细解释这个错误的原因和解决方法,帮助大家快速解决这个问题,保证项目的正常运行。
Go 1.17
GitHub.com/gin-contrib/cors v1.3.1
github.com/gin-gonic/gin v1.7.7
React应用程序放置在主域中,使用get方法和post方法访问api服务器,但出现cors策略错误 access to xmlHttprequest at 'https://
上不存在“access-control-allow-origin”标头。
在网络搜索中,我发现了同样的问题和一些解决方案,但它们对我的情况不起作用。
所有这些程序都出现相同的错误。
package gateway
import (
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func runserver() {
r := gin.default()
r.use(cors.default())
api := r.group("/api")
v1 := api.group("/v1")
userrouters(v1)
err := r.run()
if err != nil {
log.printf("failed to run gateway: %v", err)
}
}
package gateway
import (
"log"
"time"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)
func runserver() {
r := gin.default()
r.use(cors.new(cors.config{
alloworigins: []string{"*"},
allowmethods: []string{"get", "post", "put", "delete"},
allowheaders: []string{"content-type"},
allowcredentials: false,
maxage: 12 * time.hour,
}))
api := r.group("/api")
v1 := api.group("/v1")
userrouters(v1)
err := r.run()
if err != nil {
log.printf("failed to run gateway: %v", err)
}
}
响应标头中缺少 access-control-allow-origin。 · 问题 #29 · gin-contrib/cors
package gateway
import (
"log"
"github.com/gin-gonic/gin"
)
func cors() gin.handlerfunc {
return func(c *gin.context) {
c.writer.header().set("access-control-allow-origin", "*")
c.writer.header().set("access-control-allow-credentials", "true")
c.writer.header().set("access-control-allow-headers", "content-type, content-length, accept-encoding, x-csrf-token, authorization, accept, origin, cache-control, x-requested-with")
c.writer.header().set("access-control-allow-methods", "post, options, get, put, delete")
if c.request.method == "options" {
c.abortwithstatus(204)
return
}
c.next()
}
}
func runserver() {
r := gin.default()
r.use(cors())
api := r.group("/api")
v1 := api.group("/v1")
userrouters(v1)
err := r.run()
if err != nil {
log.printf("failed to run gateway: %v", err)
}
}
> curl 'https://alb.skhole.club/api/v1/authz' \
-X 'OPTIONS' \
-H 'authority: alb.skhole.club' \
-H 'accept: **' \
-H 'Accept-Language: en-US,en;q=0.9,zh-CN;q=0.8,zh;q=0.7,zh-TW;q=0.6' \
-H 'Access-Control-Request-Headers: content-type' \
-H 'Access-Control-Request-Method: POST' \
-H 'Cache-Control: no-cache' \
-H 'Connection: keep-alive' \
-H 'Origin: http://127.0.0.1:5501' \
-H 'Pragma: no-cache' \
-H 'Referer: http://127.0.0.1:5501/' \
-H 'Sec-Fetch-Dest: empty' \
-H 'Sec-Fetch-Mode: cors' \
-H 'Sec-Fetch-Site: cross-site' \
-H 'User-Agent: Mozilla/5.0 (X11; linux x86_64) AppleWEBKit/537.36 (Khtml, like Gecko) Chrome/111.0.0.0 Safari/537.36' \
--compressed -i
HTTP/1.1 204 No Content
Access-Control-Allow-Headers: Origin,Content-Length,Content-Type
Access-Control-Allow-Methods: GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Max-Age: 43200
Date: Wed, 05 Apr 2023 03:50:06 GMT
以上就是无法从主域访问子域:没有“Access-Control-Allow-Origin”的详细内容,更多请关注编程网其它相关文章!
--结束END--
本文标题: 无法从主域访问子域:没有“Access-Control-Allow-Origin”
本文链接: https://lsjlt.com/news/562721.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0