简体   繁体   中英

I want to get my textbox values from HTML document and post those when submit button is pressed using Json file in node JS

This is my JS file:-

$(document).ready(function(){

      $('form').on('submit', function(){

        var email = $("form input[type=text][name=emails]").val();
        var todo = {email: email.val(),
                     pass:dat.val()};
          $.ajax({
            type: 'POST',
            url: '/project',
            data: todo,
            success: function(data){
              //do something with the data via front-end framework
              location.reload();
            }
          });

          return false;

      });

    });

This is my html document:-

<html>
    <head>
            <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
            <script src="/assests/todo-list.js"></script>
        <body>
            <form>
                <div class="container">
                  <label><b>Email</b></label>
                  <input type="text" placeholder="Enter Email" name="emails" required>

                  <label><b>Password</b></label>
                  <input type="password" placeholder="Enter Password" name="psw" required>
                    <button type="submit" class="signupbtn">Sign Up</button>
                  </div>
                </div>
              </form>
        </body>
    </head>
</html>

I want that when i click submit button value from both text boxes gets saved in my database(using Mlab),for that i am using a json file to fire post request to the server.

My main post request handler:-

app.post('/project',parser,function(req,res)
{

var register=Todo(req.body).save(function(err,data)
{
    if(err) throw err
    res.json(data);

});
});

EDIT:-

I removed my Javascript file and now i am directly posting the form using and i am able to save my email address but not the password.

This is my main code now :-

app.post('/views/register',parser,function(req,res)
  {
     var data={
                email:req.body.emails,
                password:req.body.passcode
            };
            var send=Todo(data).save(function(err)
        {
            if(err) throw err
            res.render('login')
        })
        });

If I understand your question right, you're missing the contentType param.

$.ajax({
  type: 'POST',
  contentType: "application/json; charset=utf-8",
  ...

then try again.

I was not following the correct schema for the database when creating the object

var data={
                email:req.body.emails,
                password:req.body.passcode
            };

The property name was pass and i was using password that's why it was not inserting the password.

var data={
                email:req.body.emails,
                pass:req.body.passcode
            };

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM