为了账号安全,请及时绑定邮箱和手机立即绑定

Go gin - 从本地浏览器获取数据

Go gin - 从本地浏览器获取数据

Go
翻过高山走不出你 2022-11-23 15:54:06
我有一个用 Go Gin 编写的简单服务器:package mainimport (    "net/http"    "github.com/gin-contrib/cors"    "github.com/gin-gonic/gin")func main() {    router := gin.Default()    router.Use(cors.Default())    router.GET("/", func(context *gin.Context) {        context.JSON(http.StatusOK, gin.H{"hello": "world"})    })    router.Run(":8080")}如果我fetch从 Firefox 控制台(Ubuntu 上的 97.0.2(64 位))执行操作:fetch('http://localhost:8080/')  .then(response => response.json())  .then(data => console.log(data));我收到以下错误:Content Security Policy: The page’s settings blocked the loading of a resource at http://localhost:8080/ Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.同时,如果我使用curl从终端执行相同的 HTTP GET 请求,我会得到正确的结果:curl -X GET http://localhost:8080/{"hello":"world"}是否可以使用浏览器测试 Go gin 服务器?
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

如果您尝试从不同位置执行 ajax 请求,那么请求请求位置将阻止请求,Content Security Policy (CSP)这有助于检测和缓解某些类型的攻击。您可以在此处阅读有关 CSP的更多信息。

为了获取工作,你必须去同一个位置,并在你的情况下从那个位置发出获取http://localhost:8080/请求。

注意:由于您已经定义了在浏览器中打开位置时返回 json 的根路由,这可能会引发 GET 请求,并且响应 json 将在浏览器中返回。


查看完整回答
反对 回复 2022-11-23
  • 1 回答
  • 0 关注
  • 215 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号