Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/408.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Axios不发送cookie数据,即使使用凭据:true_Javascript_Node.js_Express_Axios - Fatal编程技术网

Javascript Axios不发送cookie数据,即使使用凭据:true

Javascript Axios不发送cookie数据,即使使用凭据:true,javascript,node.js,express,axios,Javascript,Node.js,Express,Axios,尝试使用React和Express发出请求并发送cookie。请求/响应工作正常。但是饼干没有发送。在客户端: import axios from 'axios' let endPoint = 'http://192.168.1.135:80' axios.defaults.withCredentials = true; export async function SubmitPost(username, title, body, time){ let res = await axi

尝试使用React和Express发出请求并发送cookie。请求/响应工作正常。但是饼干没有发送。在客户端:

import axios from 'axios'

let endPoint = 'http://192.168.1.135:80'
axios.defaults.withCredentials = true;

export async function SubmitPost(username, title, body, time){
    let res = await axios.post(`${endPoint}/posts/create`, {username: username, title: title, body: body, time: time})
    return res.data
}
服务器端路由:

router.post('/create', authenticator.authenticate, async (req, res) => {

    let post = new Post({
        title: req.body.title,
        body: req.body.body,
        time: req.body.time,
        username: req.body.username
    })

    post.save().then((doc, err) => {
        if (err) {
            return res.send(Response('failure', 'Error occured while posting', doc))
        }

        res.send(Response('success', 'Posted Successfully'))
    })
})
以及中间件身份验证器:

module.exports.authenticate = (req, res, next) => {
    const authHeader = req.headers['authorization']
    const token = authHeader && authHeader.split(' ')[1]

    if (token == null) {
        console.log('No authentication token found')
        return res.sendStatus(401)
    }

    jtoken.verify(token, process.env.SECRET, (err, user) => {
        if (err) {
            console.log(err)
            return res.sendStatus(403)
        }

        req.user = user
        next()
    })
}
它返回一个未找到令牌的401。我已经在express上启用了CORS以及cookie解析器

app.use(cookieparser())
app.use(cors({origin: 'http://localhost:3000', credentials: true}))
app.use(express.json())
app.use(express.urlencoded({ extended: true }))
app.use('/auth', require('./routes/authentication_route'))
app.use('/posts', require('./routes/post_route'))

带有令牌和cookie的授权头是两种不同的东西。应该注意的是如下内容-@goto1我明白了,但是cookies没有被发送的原因是什么?这将取决于cookies的设置方式-如果它们是
SameSite
cookies,而您正在执行跨站点请求,那么这可能是个问题。@goto1已经尝试将其设置为严格和无,但没有改变任何事情