Guest User

Untitled

a guest
Mar 20th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //html file
  2. //at public/index.html
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6.     <meta charset="UTF-8">
  7.     <title>Document</title>
  8. </head>
  9. <body>
  10.    <h1>Persistent Form Data</h1>
  11.    <form>
  12.       Name: <br>
  13.       <input type="text" id="name"><br>
  14.       Profession: <br>
  15.       <input type="text" id="profession">
  16.    
  17.        
  18.    </form>
  19.    
  20. </body>
  21. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  22. <script>
  23.     window.onload=function(){
  24.         $.get('/getdata',function(data){
  25.             console.log(data);
  26.             document.getElementById("name").value=data.name;
  27.             document.getElementById("profession").value=data.profession;
  28.         })
  29.     }
  30.     </script>
  31. </html>
  32.  
  33.  
  34. //server.js
  35.  
  36. const express=require('express');
  37.  
  38. var app=express();
  39.  
  40. app.get('/form',function(req,res){
  41.     res.sendFile(__dirname+'/public/index.html')
  42. });
  43.  
  44. app.get('/getdata',function(req,res){
  45.     //get the data from db and then send
  46.     res.json({name:"Harry",profession:"magician"});
  47. })
  48.  
  49.  
  50. app.listen(1234,function(){
  51.     console.log("server started at port 1234");
  52. })
Add Comment
Please, Sign In to add comment