Saturday, 6 May 2017

transfer value from client to server side node js

Tried lots of examples, spend lots of hours but it just didn't work out. It would be great if someone could help me in this situation. So, what I want to do is to get value from p#lessonText.lesson(style='margin-left:1%;' name='desc') #{item.description} I need that #{item.description} to be passed to my server side.

This is my client code

form#myForm()
  .container
    ul
      each item, index in items
        #allLessons.container
          .lessonsWrap
            h3.lesson #{index+1} . Lesson |
            p#lessonText.lesson(style='margin-left:1%;' name='desc') #{item.description}
            button#butPam.btn.btn-primary(type='submit') Begin
            .lesDescription
              p(style='margin-left:1%;') Lecturer: #{item.author}
              p(style='margin-left:1%;') Level: #{item.level}

              script.
                $('#myForm').on('submit', function(event){

                  var createVar = $('#lessonText').text();
                  alert(createVar);
                  $.ajax({
                    url: '/qVal',
                    method: 'post',
                    contentType: 'application/json',
                    data: JSON.stringify({desc: createVar.val()}),
                    async: true,
                    success: function(response){
                        console.log(response);

                    }
                  })

                });

Here in server side for now I just want to see, if I can create a variable, which is passed from client side.

router.post('/qVal', function(req,res){
     console.log("server");
     var description = req.body.desc; 
     console.log(typeof(desc));
     console.log(desc);
}

Maybe I'm using wrong method, maybe I should use GET method for this? Any help is appreciated.



via Simon Draymon

No comments:

Post a Comment