axios 请求Cookie = [对象:null prototype] {}使用Express、Cors和Cookie解析器时 您所在的位置:网站首页 axios获取数据库数据 axios 请求Cookie = [对象:null prototype] {}使用Express、Cors和Cookie解析器时

axios 请求Cookie = [对象:null prototype] {}使用Express、Cors和Cookie解析器时

2023-01-05 23:10| 来源: 网络整理| 查看: 265

我有一个快速后端设置,并通过Axios发出请求。问题是我无法访问服务器上的cookie,它是空的。服务器:

const express = require('express'); const db = require('./db') const cors = require('cors') const jwt = require('jsonwebtoken'); const dotenv = require('dotenv'); const cookieParser = require("cookie-parser"); const PORT = 3002; const app = express(); app.use(cookieParser()); app.use(express.json()) app.use(cors({ credentials: true, origin: ['http://localhost:3000', 'https://localhost:3000', 'http://localhost:3000/'],})); app.post("/api/login", (req,res)=>{ res.send(generateAccessToken(req.body.email)); console.log(req.body); }); app.post("/api/validate", (req,res)=>{ console.log(req.cookies); }); app.listen(PORT, ()=>{ console.log(`Server is running on ` + PORT); })

委托单位:

axios.post(`http://localhost:3002/api/validate`, { withCredentials: true }).then(res => { console.log(res.data); }).catch(function (error) { console.log(error); })

Cookie被存储在客户端(在应用程序存储中查找)。这是我的req. header看起来像:

host: 'localhost:3002', connection: 'keep-alive', 'content-length': '24', pragma: 'no-cache', 'cache-control': 'no-cache', 'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"', accept: 'application/json, text/plain, */*', 'content-type': 'application/json', 'sec-ch-ua-mobile': '?1', 'user-agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Mobile Safari/537.36', 'sec-ch-ua-platform': '"Android"', origin: 'http://localhost:3000', 'sec-fetch-site': 'same-site', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', referer: 'http://localhost:3000/', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7,ar;q=0.6'

我想是CORS的问题。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有