无法使用 Fetch API 针对 Django REST Framework 执行 GET 请求 - HTTP 304 错误

Cant do a GET request with Fetch API against Django REST Framework - HTTP 304 error

我正在尝试使用获取 API(节点获取)模块对 Django REST API 执行 GET 请求。但是我在执行 GET 请求时遇到错误 304。我不知道如何处理它。我读到它是关于请求与以前相同的数据。这不可能吗?或者我如何以某种方式存储这些信息?

这是我在 Express.js 中的控制器代码:

postsController.index = (req, res, next) => {
    try {
        let postsResponse = fetch('http://localhost:8000/api/posts.json', {
            method: 'GET',
            headers: {
                'Accept': 'application/json',
                "Content-Type": "application/json"
            },
        })
        .then(response => {
            return response.json()
        })
        .catch(err => {
            next(res)
        })

        postsResponse.then((result) => {
            res.render('newspapers/index', { title: 'Posts', posts: result })
        })
    } catch (error) {
        next(error)
    }
}

错误信息:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
express_1  |     at ServerResponse.setHeader (_http_outgoing.js:535:11)
express_1  |     at ServerResponse.header (/var/www/app/node_modules/express/lib/response.js:767:10)
express_1  |     at ServerResponse.contentType (/var/www/app/node_modules/express/lib/response.js:595:15)
express_1  |     at ServerResponse.send (/var/www/app/node_modules/express/lib/response.js:145:14)
express_1  |     at done (/var/www/app/node_modules/express/lib/response.js:1004:10)
express_1  |     at Object.exports.render (/var/www/app/node_modules/jade/lib/jade.js:206:5)
express_1  |     at View.exports.renderFile [as engine] (/var/www/app/node_modules/jade/lib/jade.js:233:13)
express_1  |     at View.render (/var/www/app/node_modules/express/lib/view.js:135:8)
express_1  |     at tryRender (/var/www/app/node_modules/express/lib/application.js:640:10)
express_1  |     at Function.render (/var/www/app/node_modules/express/lib/application.js:592:3)

我尝试转到 Chrome 和 select 上的网络选项卡“禁用缓存”,现在我得到 200 OK,但由于某种原因我仍然得到上面那个问题的回溯。这是什么意思?

>300 范围内的错误与重定向相关。

重定向:'关注

因此您可能应该遵循您的重定向,或者如果这种重定向行为不是有意为之,那么您在下游服务中配置错误。

        let postsResponse = fetch('http://localhost:8000/api/posts.json', {
            method: 'GET',
            headers: {
                'Accept': 'application/json',
                "Content-Type": "application/json"
            },
+++         redirect: 'follow'
        })
``