Agregué datos a html a través de NodeJS Express, y con .setHeader, obtuve el título html como json. Extraiga datos de html en el archivo JS y envíelos a otro archivo html solo como título para cada título.
Está el código JavaScript,
$(document).ready(function () { $.get("/admin/getposts", null, function (data) { // Main bir div oluşturmak $(".postsMain").html(""); $.each(data, function (i, item) { // Div 2 için html ayarlamak var html += "<div class='postsAlt'>"; html += "<p>" + i.title + "</p>" html += "</div>"; // Div 2'leri div1 lere aktarmak $(".postsMain").append(html); }); }, "json").done(function() { console.log('Başarıyla data çekildi') }) });Hay datos de extracción en NodeJS
app.get("/admin/getposts", (req, res) => { let titles = []; Object.entries(postData.all()).forEach((entry) => { const [ID, data] = entry; titles.push(data.data) }); res.setHeader('Content-Type', 'application/json') res.send(JSON.stringify(titles)); });Algunas correcciones a su código.
$(function () { $.getJSON("/admin/getposts", function (data) { $(".postsMain").empty(); $.each(data, function (i, item) { var alt = $("<div>", { class: "postsAlt" }).appendTo(".postsMain"); $("<p>").html(item.title).appendTo(alt); console.log('Başarıyla data çekildi'); }); }); }); En su ciclo Cada, usó i para el índice y item para el valor. Entonces i.item debería haber arrojado un error sobre no estar definido. Lo cambié a item.title que debería dar el detalle adecuado.
También actualicé el código para usar jQuery para crear los nuevos elementos HTML.