簡體   English   中英

如何從客戶端將 HTML 文檔元素發送到 Node JS 中的服務器?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM