javascript - How to pass value from a form control via ajax to node.js -


i new jquery/node.js world. so, trying basic stuff getting stuck. checked questions not able hold of right approach.

so, here posting code understand wrong here:

i using express framework view template engine build using ejs.

this script have written in index.ejs

<script>     $(document).ready(function() {         $("button1").click(function() {             var obj = $('#text1').val();             $.post("/localhost:3000/",{val: obj}, function(restxt, status, xhr) {                   if (status=="success") {                     alert("external content loaded successfully");                     //code                   }                   if (status=="error") {                      alert(xhr.status + xhr.statustext);                      //code                   }                                                       })           })       });   </script> 

and on index.js render html ejs, have written following:

  router.get('/', function(req, res, next) {          res.render('index', { title: 'express' });     if (req.form("val")!='') {         console.log("we got data: " + req.form("val"));         res.send("ack");     } else {         console.log("still no data");     }  }); 

thanks,

nishith

add following under router.get(...);

router.post('/', function(req,res,next){     var val = req.body.val;     res.send('got val'); }); 

Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -