doulong6761 2015-11-29 02:56
浏览 233
已采纳

添加Vue HTTP标头时出错

I have a working application with a front end in vuejs and a backend in go. I'm in the process of adding the final features to the authentication and I'm running into an error.

When I try to add any http header to the vuejs request I get an error.

This is what I'm adding to the vue component:

http: {
  headers: {
    Authorization: 'Bearer 123'
  }
}

And I'm getting a preflight error:

XMLHttpRequest cannot load http://localhost:8000/api/investments?limit=6. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

Now I know this is usually a server side problem but I am adding the response header resp.Header().Set("Access-Control-Allow-Origin", "*"). cors. If I remove the the http property in the vue object everything works fine. Its pretty bizzare I even tried handling the OPTIONS request with go but my handler for the api doesn't even get hit.

  • 写回答

1条回答 默认 最新

  • drkug66408 2015-11-29 03:05
    关注

    Ok so I found the answer it is a server problem the Authorization header was not authorized. So just add it as a middleware to allow for it.

    c := cors.New(cors.Options{
            AllowedHeaders: []string{"Origin", "Accept", "Content-Type", "Authorization"},
        })
    handler := c.Handler(router)
    log.Fatal(http.ListenAndServe(":8000", handler))
    

    I am using the rs/cors package

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘