javascript - 从回调函数内部使用 "pure"Node.js 重定向

标签 javascript node.js ajax redirect server

以下是我的 server.js 代码的 MCVE:

let fs = require('fs');
let http = require('http');

http.createServer((req, res) => {
    // Handles GET requests
    if(req.method == 'GET') {
        let file = req.url == '/' ? './index.html': '/login.html'; // just an example
        fs.readFile(file, (err, data) => {
            res.writeHead(200, {'Content-Type': 'text/html'});
            res.end(data);
        });
    } 

    // Handles POST requests
    else {
        read(status => {
            if(status) {
                res.writeHead(302, {
                    'Location': 'http://localhost:8000/login.html',
                    'Content-Type': 'text/html'
                });
                res.end();
                console.log('Redirected!');
            }
        });
    }
}).listen(8000);

// In my actual script, the `read` function reads JSON files and sends data,
// so I've used the callback function
let read = callback => fs.readFile( './index.html', (err, data) => callback(true) );

而且,我有两个代码中提到的 HTML 文件。

index.html

<input type="submit" onclick='let xhr = new XMLHttpRequest(); xhr.open("POST", "http://localhost:8000"); xhr.send();'>

我使用内联脚本来最小化我的 MCVE 中的流量。出于开发目的,我将在我的网站上使用外部脚本

login.html

<h1>Login</h1>

现在,当我打开 http://localhost 时,index.html 会很好地显示出来。您已经注意到 index.html 只是一个按钮。因此,当我单击该按钮时,Ajax 请求被成功触发并且一切正常(没有控制台错误),除了页面不重定向这一事实。我不知道它出了什么问题或还缺少什么。

我是 Node.js 的初学者,在 Nodejs - Redirect url 中阅读了有关重定向的内容。和 How to redirect user's browser URL to a different page in Nodejs? ,我搜索了很多,但无法得到提示。感谢您的宝贵时间!

Also, I'm aware about express, but I don't consider use of frameworks as they hide core concepts.


编辑:当我尝试在没有回调概念的情况下进行重定向时,它可以正常工作,如 this video告诉我们。

最佳答案

这不是 node.js 的问题。这就是浏览器的行为方式。

Ajax (XHR) 不会在浏览器中触发重定向。当浏览器实现 XHR 时,浏览器开发人员假设您想要控制页面刷新行为。因此,他们确保 XHR 不会触发任何默认操作。所有重定向都将保持沉默,并且重定向的结果数据将传递给您的 XHR 对象的 onreadystatechange 回调。

如果您希望重定向触发页面刷新,您可以简单地选择不使用 XHR。而是提交表单:

<!-- index.html -->
<form action="http://localhost:8000" method="post">
    <input type="submit">
</form>

如果您想使用 AJAX,则需要像我上面提到的那样在浏览器中进行重定向:

// server.js

// ...

http.createServer((req, res) => {

    // ...

    // Handles POST requests
    else {
        read(status => {
            if(status) {
                res.writeHead(200, {'Content-Type': 'application/json'});
                res.end(JSON.stringify({
                    your_response: 'here'
                }));
            }
        });
    }
}).listen(8000);

然后在浏览器中处理该响应:

index.html

<input type="submit" onclick='let xhr = new XMLHttpRequest();xhr.addEventListener("load", function(){var response = JSON.parse(this.responseText);/* check response if you need it: */if (response.your_response === 'here') {window.location.href = 'http://localhost:8000/login.html';}});xhr.open("POST", "http://localhost:8000");xhr.send();'>

但这非常丑陋,几乎无法阅读。我建议将该 HTML 重构为如下内容:

<!-- index.html -->

<script>
    function handleSubmit () {
      let xhr = new XMLHttpRequest();
      xhr.addEventListener("load", function(){
        var response = JSON.parse(this.responseText);

        /* check response if you need it: */
        if (response.your_response === 'here') {
          window.location.href = 'http://localhost:8000/login.html'; // REDIRECT!!
        }
      });
      xhr.open("POST", "http://localhost:8000");
      xhr.send();
    }
</script>

<input type="submit" onclick="handleSubmit()">

关于javascript - 从回调函数内部使用 "pure"Node.js 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53933426/

相关文章:

node.js - 是否可以每 *4 分钟* 在 Heroku 中运行一个 Node.js 应用程序?

php - 如何在 ajax 脚本中将变量从表单传递到 php 页面?

php - 我的 AJAX 应用程序总是返回 "found"

javascript - Nuxt/Vue - 它生成的 html 和 js 文件

javascript - HTML 单选按钮

javascript - 在 localStorage 中存储和检索动态填充的下拉列表

javascript - 使用 javascript 检测对 -webkit-filter 的支持

node.js - nodejs , socket.io 简单的代码内存泄漏

javascript - 运行 node.js mocha 测试时意外的 token 非法

javascript - AJAX 表单未发送并且没有错误代码