javascript - 如何在 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 的下一页。
解决方案
当您需要在字符串中插入变量时
在 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>`);
推荐阅读
- c# - 在创建列表时调用重载的构造函数
- c++ - 将 find 与 QAssociativeIterable 或 QSequentialIterable 一起使用,同时避免在 Qt5 中复制
- reactjs - 下一个构建命令失败:错误命令失败,退出代码为 1
- android - 信号 11 (SIGSEGV),代码 1 (SEGV_MAPERR) librealm-jni.so
- ibm-cloud-infrastructure - 查询以获取服务器列表
- java - Spring MVC 控制器将表情符号解析为 CJK 字符
- reactjs - RN:构造函数与没有构造函数的组件道具
- reactjs - 将隐藏组件添加到 jspdf
- mysql - 我应该使用 Ruby 进行计算,还是应该使用 MySQL 进行计算?
- c# - Visual Studio 2019 中的“ildasm”在哪里?