首页 > 解决方案 > 如何在 Node.js 中的 HTML 页面上显示 API 响应?

问题描述

如何在 Node.js 中的 HTML 页面上显示 API 响应?

这是我的代码:

 res.writeHead(200, {'Content-Type': 'text/html'});
                res.write('Code:'+req.body);
                var temp = req.body;
                console.log(req.body)
                res.write('<script>setTimeout(function () { window.location.href = "http://localhost:63342/untitled/node_rest_shop/api/login_success.html?_ijt=f37kuelpb06k09ooesdrced6ko" + res.body},5000);</script>');

                res.end();

响应成功后,应显示 HTML 的下一页。

标签: javascriptnode.js

解决方案


当您需要在字符串中插入变量时

在 Javascript 中,它调用Template_literals使用反引号而不是单引号

res.write(`<script>setTimeout(function () { window.location.href = "http://localhost:63342/untitled/node_rest_shop/api/login_success.html?_ijt=f37kuelpb06k09ooesdrced6ko${res.body}"},5000);</script>`);

推荐阅读