执行返回res.send()的Javascript代码

execute Javascript code that is returned res.send()

本文关键字:Javascript 代码 返回 res send 执行      更新时间:2023-11-22

我正在返回(使用Node.js)this:

var template = '<a href="http://localhost:7000/">Your Account has been activated. </a>';
return res.send(template);

我需要做的是返回最小的javascript代码,它将进行自动重定向,而不是标记。

到目前为止,用户必须点击链接,我需要它自动重定向。

想法?

我假设您使用ajax,并在响应中返回HTML。您只需返回javascript:

var template =  '<script type="text/javascript"> window.location.href="http://localhost:7000"; </script>';
return res.send(template);

然后将其附加到您的文档中。但这不是最干净的解决方案。

您可以返回链接,并在ajax完整回调中使用它。

您可以设置位置头:

res.setHeader('Location', 'http://someurl.com');

nodejs http docs