登录
首页 >  Golang >  Go问答

源"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:",
        "secure": false,
        "loglevel": "debug"
    }
}

并在 angular.json 中引用此文件:

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": ":build",
            "proxyConfig": "src/proxy.conf.json"
          },
          ...
        },

今天关于《源"http://localhost:4200"无法访问"http://localhost:8080/BeginSignup",原因是 CORS 策略阻止》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>