A
A
anakondoz2020-05-06 14:43:27
Node.js
anakondoz, 2020-05-06 14:43:27

Why is req.xhr always false?

There is a test SPA site, the content is rendered by axios. Everything works fine, and when you would like to redirect to the main page during a normal page refresh, if not an hxr request, the problem is that req.xhr is always false

app.get('/test', function (req, res) {

// получаю с БД данные
//....

// только так пока что добился редиректа если не xhr
    if (res.type('xhr').req.headers.accept === 'application/json, text/plain, */*') {
        res.set('X-Requested-With', 'XMLHttpRequest')
        res.render('test', {
            title: 'Test',
            test: result, // в шаблон pug данные из бд
        })
        console.log(req.xhr) // false
    } else {
        console.log(req.xhr)
        res.redirect('/')
    }
});


I saw that the type is xhr and the X-Requested-With header is set, but console.log(req.xhr) // false. I can't understand why this is so, and is there another way to determine the request type in express?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question