[英]How to send HTML document element to server in Node JS from client?
我正在尝试捕捉我的客户端文档对象并将其发送到节点 js 服务器。
但是当我尝试将
JSON.stringify(document.documentElement)
我不能这样做。 它变成了一个空对象。
我想将客户端文档对象保存为服务器端的 HTML 文件,在那里进行一些小修改(替换相关链接和所有内容)并在我们访问服务器时将其返回。 那我该怎么做呢?
这是我在客户端尝试的
if (request.action == "take_snap") {
var base = document.querySelector('base');
base.remove();
var doc = document.documentElement;
const req = new XMLHttpRequest();
const baseUrl = "http://localhost:3000/";
req.open("POST", baseUrl, true);
req.setRequestHeader("Content-type", "application/json");
req.send(JSON.stringify(doc));
req.onreadystatechange = function() { // Call a function when the state changes.
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
console.log("Got response 200!");
}
}
}
这里还有其他可行的方法吗? 或者我如何实现我正在尝试的任何事情。 请帮忙。
JSON.stringify()
期望JSON
作为将其转换为字符串的参数。 您正在阅读的 html 不是JSON
。您可以尝试从document.documentElement
中删除JSON.stringify()
并检查 nodejs 服务器的日志。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.