源"http://localhost:4200"无法访问"http://localhost:8080/BeginSignup",原因是 CORS 策略阻止
来源:stackoverflow
时间:2024-03-01 09:12:26 296浏览 收藏
怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《源"http://localhost:4200"无法访问"http://localhost:8080/BeginSignup",原因是 CORS 策略阻止》,涉及到,有需要的可以收藏一下
问题内容
我正在尝试向我的 golang appengine api 发送 fetch post 请求。但是,我收到 cors 策略错误,但我不知道为什么。这是我的代码:
这是从 angular 调用 api 的代码
private async sendhttpput(data: userinfo, func: string) {
let requestheaders = { 'content-type': 'application/json', 'access-control-allow-origin': '*' } as headersinit;
console.log(requestheaders)
return await fetch(
this.endpoint + func,
{
method: 'post',
headers: requestheaders,
body: json.stringify(data),
mode: "cors"
}
)
}
这是处理 cors 的服务器代码:
func startapp() {
router = mux.newrouter()
router.handlefunc("/", hello)
router.handlefunc("/hello", hello)
router.handlefunc("/acceptmember", acceptmember)
router.handlefunc("/createteam", createteam)
router.handlefunc("/leave", leave)
router.handlefunc("/invitemember", invitemember)
router.handlefunc("/beginsignup", beginsignup)
router.handlefunc("/processsignup", processsignup)
router.handlefunc("/completesignup", completesignup)
// scoring
router.handlefunc("/getlocalscore", getlocalscore)
allowedorigins := handlers.allowedorigins([]string { "* always" })
allowedheaders := handlers.allowedheaders([]string { "content-type" })
if err := http.listenandserve(":8080", handlers.cors(allowedorigins, allowedheaders)(router)); err != nil {
log.fatal(err)
}
}
这是发送到服务器的请求:
request url: http://localhost:8080/beginsignup
referrer policy: no-referrer-when-downgrade
provisional headers are shown
access-control-allow-origin: *
content-type: application/json
referer: http://localhost:4200/
user-agent: mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/85.0.4183.121 safari/537.36 edg/85.0.564.68
payload
{member: {phonenum: "0648503047"},…}
member: {phonenum: "0648503047"}
score: null
team: {teamname: ".none", teammembers: ["0648503047"], teamleader: "0648503047"}
这是从服务器发回的响应:
Request URL: http://localhost:8080/BeginSignup Request Method: OPTIONS Status Code: 200 OK Remote Address: [::1]:8080 Referrer Policy: no-referrer-when-downgrade Content-Length: 0 Date: Mon, 05 Oct 2020 20:34:51 GMT Accept: */* Accept-Encoding: gzip, deflate, br Accept-Language: en-US,en;q=0.9 Access-Control-Request-Headers: access-control-allow-origin,content-type Access-Control-Request-Method: POST Connection: keep-alive Host: localhost:8080 Origin: http://localhost:4200 Referer: http://localhost:4200/ Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors Sec-Fetch-Site: same-site User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36 Edg/85.0.564.68
解决方案
我假设您在开发模式下运行 angular (ngserve)。
在这种情况下,cors 错误来自角度。
您必须添加一个 proxy.conf.json,其中包含 src 中不同端点的条目:
{
"/api/*": {
"target": "http://localhost:<your-port>",
"secure": false,
"loglevel": "debug"
}
}
并在 angular.json 中引用此文件:
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "<your-app>:build",
"proxyConfig": "src/proxy.conf.json"
},
...
},今天关于《源"http://localhost:4200"无法访问"http://localhost:8080/BeginSignup",原因是 CORS 策略阻止》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
478 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 543次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 516次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 500次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 485次学习