Friday, 17 March 2017

How to make page change with GET request in JavaScript with Node.js and Express?

In this app, the user has a table they can enter data into. Each row has an "Edit" button, and when they click that button, I want to take them to a new page called "/update" where they can modify the values in that row.

I have it now so that when they click the the update button, it runs the editRow function, passing along the id of the table, and the data in the row. However, I'm having trouble figuring out how to get the routes to work correctly.

I'm using Node.js, Express, Handlebars but no jQuery.

app.js (server side)

app.get('/update', function(req,res){
    var context = {};
    context.tableID = req.ID;
    context.element = req.element;
    res.render('update', context);
});

update.handlebars

<h1>test for now</h1>

main.handlebars

<!doctype html>
<html>
<head>
    <title>Workout Tracker</title>
    <link rel="stylesheet" href="css/style.css">
    <script src="scripts/buttons.js" type="text/javascript"></script>
</head>
<body>
    }
</body>
</html>

buttons.js (client side)

function editRow(tableID, element) {

    var req = new XMLHttpRequest();
    payload.tableID = tableID;
    payload.element = element;

    req.get("GET", "http://localhost:3000/update", false);    

    req.setRequestHeader('Content-Type', 'application/json');

    req.send(JSON.stringify(payload));

    req.send(payload);
}



via Taylor Liss

No comments:

Post a Comment