无法显示 JSON 数据在 AG 格网中,只是显示加载中
由于浏览器安全限制,跨域 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学习网公众号。
深入探讨Linux归档功能并提供实践指南
- 上一篇
- 深入探讨Linux归档功能并提供实践指南
- 下一篇
- Linux Bashrc指南:功能、配置和用法
-
- Golang · Go问答 | 11小时前 |
- Go context.AfterFunc 返回 false 时说明清理已发生吗
- 262浏览 收藏
-
- Golang · Go问答 | 11小时前 | sync · 错误处理 · go · Go error sync.OnceFunc sync.OnceValue sync.OnceValues
- Go sync.OnceFunc 与 OnceValue 的错误返回设计
- 416浏览 收藏
-
- Golang · Go问答 | 11小时前 | Go 资源清理 sync.Once context.AfterFunc 取消回调
- Go context.AfterFunc 回调与取消同时发生时怎样保证只清理一次
- 390浏览 收藏
-
- Golang · Go问答 | 11小时前 | Go问答 · Go map Slice sync.Once 并发初始化 sync.OnceValue
- Go sync.OnceValue 返回的函数为什么不适合返回可变全局
- 116浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go atomic.Pointer 更新对象后为什么旧对象还不能立刻释放
- 162浏览 收藏
-
- Golang · Go问答 | 11小时前 | Go sync/atomic int64 Uint64 整数溢出
- Go atomic.Uint64 和 atomic.Int64 在溢出时怎么解释结果
- 423浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go clear 对 nil map 和空 map 的行为有什么区别
- 283浏览 收藏
-
- Golang · Go问答 | 11小时前 |
- Go mapclear 后旧引用为什么仍保留部分内存
- 167浏览 收藏
-
- Golang · Go问答 | 11小时前 | 迭代器 · Go问答 · Go yield iter.Seq range over function iter.Pull
- Go iter.Seq 怎样让消费者提前停止生产
- 193浏览 收藏
-
- Golang · Go问答 | 11小时前 | Go 容量 迭代器 slices.Collect 内存成本
- Go slices.Collect 收集迭代器后怎样估算内存成本
- 499浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 485次学习
-
- PubMedQA
- 深入了解PubMedQA生物医学问答数据集,涵盖其核心功能、使用方法及在临床决策、药物研发等场景的应用,助力提升NLP模型性能。
- 1021次使用
-
- H2O EvalGPT
- H2O EvalGPT是H2O.ai推出的开源LLM评估平台,提供详细的大模型性能排行榜、行业特定基准测试及A/B测试功能,助您快速选择最适合项目的高性能大语言模型。
- 1023次使用
-
- LMArena
- LMArena是加州大学伯克利分校推出的AI模型匿名评测平台。通过盲测投票机制,用户可对比不同大模型回答并生成实时排行榜,助力开发者优化模型及用户选择最佳AI工具。
- 1160次使用
-
- HELM
- 深入了解斯坦福推出的HELM(Holistic Evaluation of Language Models)大模型评测体系。本文解析其核心功能、安装配置步骤及应用场景,涵盖准确性、公平性、鲁棒性等多维度指标,助力开发者全面优化语言模型性能。
- 1126次使用
-
- MMBench
- MMBench是由上海人工智能实验室等机构联合推出的多模态基准测试平台,提供细粒度能力评估、大规模数据集及VLMEvalKit工具。本文详细介绍其核心功能、安装使用方法及应用场景,助力开发者全面评估多模态模型性能。
- 966次使用
-
- 用Nginx反向代理部署go写的网站。
- 2023-01-17 502浏览
-
- GoLand调式动态执行代码
- 2023-01-13 502浏览
-
- Go types.Selection.Type 与 Obj.Type 为什么不总是相同
- 2026-11-10 501浏览
-
- Go flag.FlagSet ContinueOnError 怎么捕获帮助输出和解析错误
- 2026-11-05 501浏览
-
- Go netip.ParseAddr 为什么拒绝带端口字符串
- 2026-11-03 501浏览

