简体   繁体   中英

How to send HTML document element to server in Node JS from client?

I am trying catch a snap of my client side document object and send it across to the node js server.

But when I try to stringify the

JSON.stringify(document.documentElement)

I am not able to do so. It becomes an empty object.

I want to save the client side document object as an HTML file in server side, do some minor modifications there(replacing relative links and all) and serve it back when we hit our server. How do I do it then ?

here is what I am trying on client side

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!");
            }
        }
    }

Any other viable approach here ? Or how do I achieve whatever I am trying. Please help.

JSON.stringify() expects JSON as an argument which converts it into a string. The html you are reading isn't JSON .Can you try removing the JSON.stringify() from the document.documentElement and check the logs of the nodejs server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM