Answer the question
In order to leave comments, you need to log in
Fetch - how to change a single element without reloading the entire page?
<body>
<h1>HI</h1><p>Response succesed</p>
<input id="button" type="submit" value="Отправить" onclick="fPost()">
</body>
async function fPost(){
let response = await fetch("http://localhost:5000/", {
method: 'POST'
});
let dom = await response.json();
console.log(dom);
}
const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');
let html;
let js;
fs.readFile('./index.html',(err, data) => {
if (err){
throw err;
}
html = data;
});
fs.readFile('./one.js',(err, data) => {
if (err){
throw err;
}
js = data;
});
const server = http.createServer();
server.on('request', (req, res) => {
if (req.url === '/' && req.method === 'GET'){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}
if (req.url === '/one.js'){
res.writeHead(200, {'Content-Type': 'text/javascript'});
res.end(js);
}
console.log(req.method);
if (req.method === 'POST' && req.url === '/'){
res.writeHead(200, {'Content-Type': 'application/json'});
//res.write(html);
res.write(jsonDom);
console.log("<p>json!!!</p>");
res.end();
}
});
server.listen(5000);
console.log("Слушаю порт 5000");
let dom =
'let p = document.createElement("p");' +
'p.textContent = "Hello";' +
'document.body.appendChild(p);';
let jsonDom = JSON.stringify(dom);
console.log(jsonDom);
Answer the question
In order to leave comments, you need to log in
force the site to change the dom structure after receiving a response from the server
Request - response happens through ajax. On the client side, there is js that changes the DOM. Usually they take react / vue / angular, but there are situations that it’s easier and more reasonable to write in vanilla. Vanilla can, and was created for this, work with the DOM. There are methods on the document object, such as createElement, appendChild, insertAdjacentElement, and so on.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question