我正在使用express.js。我想做的只是在单击表元素后重定向到 html 页面。目前输出是 HTML 源 console.log(而不是重定向然后解释为 HTML 页面)
我的点击函数如下(ajax post)
<script>
$(".table").click(function() {
/* Act on the event */
$.ajax({
url: "/selectForm",
type: "POST",
data: {name : "John"},
cache: false,
timeout: 5000,
complete: function() {
//called when complete
console.log('process complete');
},
success: function(data) {
console.log(data);
console.log('process sucess');
},
error: function() {
console.log('process error');
},
});
});
我的 app.post 如下所示简单;
app.post('/selectForm',function(req,res){
res.redirect('hello.html');
});
我在 Chrome javascript 控制台中获得了 HTML 输出,但在浏览器中我没有看到它重定向到 hello.html。
最佳答案
您的 res.redirect('hello.html')
指令不会影响 AJAX 请求。 AJAX 是异步的,因此将向服务器发出请求,并将响应返回到浏览器以由 JavaScript 进行解释。
要在 AJAX 请求后重定向到另一个页面,您需要在 console.log 下方给出额外的指令 (
指令。window.location.replace('hello.html')
) ('处理完成')
关于jquery - 在 Node.js 中使用 ajax post 后,HTML 页面未呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23607271/