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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵