我在 NodeJS 中使用 express 框架。当客户端发出 POST 请求时,它会将一些工作推送到 python 脚本,然后将 JSON 对象返回到客户端网页。
我的问题是,每当我的 POST 请求完成时,客户端就会刷新。我已经对 Axios 和 Fetch http 客户端进行了测试。在发出这样的 POST 请求时,如何防止我的网页刷新?
服务器代码如下所示:
app.post("/data/", function(req, res) {
const spawn = require("child_process").spawn;
const pythonProcess = spawn('python', ["my_script.py"]);
pythonProcess.stdout.on('data', (data) => {
let result = data.toString();
res.send(JSON.stringify(result));
});
});
Run Code Online (Sandbox Code Playgroud)
然后我从我的 React 客户端发出请求(使用 Axios,但 Fetch 的行为方式相同):
axios({
method: 'post',
url: `http://localhost:3001/videos/?url=${url}`,
responseType: 'json',
});
Run Code Online (Sandbox Code Playgroud)
并且客户端网页刷新...
我知道 React 不应该受到责备,因为没有任何内容更新应用程序状态。即使我对来自服务器的响应不做任何处理,当 POST 完成时网页仍然会刷新。
此外,我的网页上没有表单元素或类似的东西,我button的类型是 typebutton而不是 type submit。
我有一种预感,这与调用 python 进程本身的方式有关,因为如果我只返回一个普通的 JSON …