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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题