Api 在Vue页面上未检测到Cookie

Api 在Vue页面上未检测到Cookie,api,vue.js,go,cookies,Api,Vue.js,Go,Cookies,每当用户发出API请求时,我都会检查会话是否有效 userCookie, err := r.Cookie("session-id") if err != nil { fmt.Println(err) if strings.HasPrefix(r.URL.Path, "/login") { Login(w, r, db) //login and create session } } else { //A

每当用户发出API请求时,我都会检查会话是否有效

userCookie, err := r.Cookie("session-id")
    if err != nil {
        fmt.Println(err)
        if strings.HasPrefix(r.URL.Path, "/login") {
            Login(w, r, db) //login and create session
        }
   } else {
     //Authenticate Session Then if Valid proceed to my APIs
   }
因此,当我直接在浏览器的搜索栏上使用API时,它会起作用,因为它检测到会话cookie,但当我在Vue/axios上使用API时,它不会检测到cookie,并且会出现以下错误:

http: named cookie not present
exit status 1
当我创建会话时,我设置cookie
路径:“/”
,这样即使我的服务器和前端有不同的端口,会话cookie也会生成到我的Vue页面那么,如何让服务器在我的Vue页面上检测我的会话cookie?

您需要设置在CORS请求上发送cookie(并且由于您有不同的端口,因此它被视为不同的服务器)

您需要设置在CORS请求时发送Cookie(由于您有不同的端口,所以它被视为不同的服务器)

axios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    config.withCredentials = true;
    return config;
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error);
  }
);
axiox.get('/', { withCredentials: true })