无法显示 JSON 数据在 AG 格网中,只是显示加载中
来源:stackoverflow
时间:2024-03-20 10:18:29 293浏览 收藏
由于浏览器安全限制,跨域 HTTP 请求被阻止,导致 Angular 应用程序无法从本地 Go 服务中获取 JSON 数据。要解决此问题,需要在 Go 服务器中启用 CORS(跨域资源共享)支持,允许应用程序从不同的域访问数据。这可以通过使用 gorilla/handlers 或 rs/cors 库来实现,它们允许在响应中设置适当的 CORS 标头。
使用我在网上找到的各种示例,我拼凑了一个简单的网络应用程序。然而,尽管 ag grid(我选择用来显示数据的网格)可以与提供的数据源配合使用,但它不能与我自己的数据源配合使用,该数据源是使用 go 编写的 web 服务创建的。
角度代码...
ngoninit() { this.rowdata = this.http.get('https://api.myjson.com/bins/ly7d1'); }
这可以正确显示网格上的数据。但是当我将其重定向到我时,go 使用以下命令生成数据...
ngoninit() { this.rowdata = this.http.get('http://localhost:10000/all'); }
这个网格只是说正在加载...
如果我在浏览器中测试任一链接,我会得到以完全相同的方式格式化的完全相同的数据...
[{"make":"toyota","model":"celica","price":35000},{"make":"ford","model":"mondeo","price":32000},{"make":"porsche","model":"boxter","price":72000},{"make":"toyota","model":"celica","price":35000},{"make":"ford","model":"mondeo","price":32000},{"make":"porsche","model":"boxter","price":72000},{"make":"toyota","model":"celica","price":35000},{"make":"ford","model":"mondeo","price":32000},{"make":"porsche","model":"boxter","price":72000},{"make":"toyota","model":"celica","price":35000},{"make":"ford","model":"mondeo","price":32000},{"make":"porsche","model":"boxter","price":72000}]
这是 json 的链接:
https://api.myjson.com/bins/ly7d1
我在同一台机器上运行我的 angular 应用程序和 go 应用程序,但有不同的服务并使用不同的端口...
我可以包含 go 代码,但我不知道它有多相关,因为数据在浏览器中正确显示。
尝试只包含相关的内容,但如果我遗漏了某些内容,请告诉我,我可以上传。
html 代码...
welcome to {{ title }}!
![]()
here are some links to help you start:
预期输出...
go web 服务代码...
package main import ( "database/sql" "encoding/json" "fmt" "log" "net/http" "github.com/gorilla/mux" _ "github.com/go-sql-driver/mysql" ) // Article - Our struct for all articles type Article struct { Make string `json:"make"` Model string `json:"model"` Price int32 `json:"price"` } type Articles []Article func homePage(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Welcome to the HomePage!") fmt.Println("Endpoint Hit: homePage") } func returnAllArticles(w http.ResponseWriter, r *http.Request) { articles := Articles{ Article{Make: "Toyota", Model: "Celica", Price: 35000}, Article{Make: "Ford", Model: "Mondeo", Price: 32000}, Article{Make: "Porsche", Model: "Boxter", Price: 72000}, Article{Make: "Toyota", Model: "Celica", Price: 35000}, Article{Make: "Ford", Model: "Mondeo", Price: 32000}, Article{Make: "Porsche", Model: "Boxter", Price: 72000}, Article{Make: "Toyota", Model: "Celica", Price: 35000}, Article{Make: "Ford", Model: "Mondeo", Price: 32000}, Article{Make: "Porsche", Model: "Boxter", Price: 72000}, Article{Make: "Toyota", Model: "Celica", Price: 35000}, Article{Make: "Ford", Model: "Mondeo", Price: 32000}, Article{Make: "Porsche", Model: "Boxter", Price: 72000}, } fmt.Println("Endpoint Hit: returnAllArticles") json.NewEncoder(w).Encode(articles) } type Tag struct { JN string `json:"jobno"` Title string `json:"title"` } func returnSingleArticle(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) key := vars["id"] fmt.Fprintf(w, "Key: "+key) } func handleRequests() { myRouter := mux.NewRouter().StrictSlash(true) myRouter.HandleFunc("/", homePage) myRouter.HandleFunc("/all", returnAllArticles) log.Fatal(http.ListenAndServe(":10000", myRouter)) } func main() { handleRequests() }
更新 我现在已经尝试了以下... 完全重写了我的 macbook 上的网站和服务 尝试在服务器和客户端上使用不同的端口 尝试在另一台机器上运行服务器 禁用所有防火墙
这些都没有任何区别。
解决方案
您无法获取数据,因为出于安全原因,您的浏览器阻止了从 http://localhost:4200
到 http://localhost:10000/all
的跨域 http 请求。您的 go 服务器必须能够处理预检 options
请求并在响应中发送正确的 CORS 标头。
使用 gorilla/handlers
或 rs/cors
启用 cors 支持。
import ( "net/http" "github.com/gorilla/mux" "github.com/gorilla/handlers" "github.com/rs/cors" ) func handleRequests() { myRouter := mux.NewRouter().StrictSlash(true) myRouter.HandleFunc("/", homePage) myRouter.HandleFunc("/all", returnAllArticles) // ----- OPTION 1 ----- Use rs/cors corsOptions := cors.New(cors.Options{ AllowedHeaders: []string{"X-Requested-With", "Content-Type"}, AllowedOrigins: []string{"*"}, // instead of '*' you can add the urls you want to allow e.g. 'http://localhost:4200' AllowedMethods: []string{http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodOptions, http.MethodHead} }) log.Fatal(http.ListenAndServe(":10000", corsOptions.Handler(myRouter)) // -------------------------------- // ----- OPTION 2 ----- Use gorilla/handlers corsHeaders := handlers.AllowedHeaders([]string{"X-Requested-With", "Content-Type"}) corsOrigins := handlers.AllowedOrigins([]string{"*"}) corsMethods := handlers.AllowedMethods([]string{"GET", "HEAD", "POST", "PUT", "OPTIONS"}) log.Fatal(http.ListenAndServe(":10000", handlers.CORS(corsHeaders, corsOrigins, corsMethods)(myRouter))) // -------------------------------- } func main() { handleRequests() }
以上就是本文的全部内容了,是否有顺利帮助你解决问题?若是能给你带来学习上的帮助,请大家多多支持golang学习网!更多关于Golang的相关知识,也可关注golang学习网公众号。
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习