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 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 如何卸载arcgis 10.1 data reviewer for desktop
  • ¥15 共享文件夹会话中为什么会有WORKGROUP
  • ¥15 关于#python#的问题:使用ATL02数据解算光子脚点的坐标(操作系统-windows)
  • ¥115 关于#python#的问题:未加密前两个软件都可以打开,加密后只有A软件可打开,B软件可以打开但读取不了数据