Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //html file
- //at public/index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Document</title>
- </head>
- <body>
- <h1>Persistent Form Data</h1>
- <form>
- Name: <br>
- <input type="text" id="name"><br>
- Profession: <br>
- <input type="text" id="profession">
- </form>
- </body>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <script>
- window.onload=function(){
- $.get('/getdata',function(data){
- console.log(data);
- document.getElementById("name").value=data.name;
- document.getElementById("profession").value=data.profession;
- })
- }
- </script>
- </html>
- //server.js
- const express=require('express');
- var app=express();
- app.get('/form',function(req,res){
- res.sendFile(__dirname+'/public/index.html')
- });
- app.get('/getdata',function(req,res){
- //get the data from db and then send
- res.json({name:"Harry",profession:"magician"});
- })
- app.listen(1234,function(){
- console.log("server started at port 1234");
- })
Add Comment
Please, Sign In to add comment