我想从HTML文档中获取我的文本框值,并在使用节点JS中的Json文件按下提交按钮时发布这些值

时间:2022-09-25 16:08:51

This is my JS file:-

这是我的JS文件: -

$(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文档: -

<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.

我希望当我点击两个文本框中的提交按钮值时,保存在我的数据库中(使用Mlab),因为我正在使用json文件向服务器发送post请求。

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.

我删除了我的Javascript文件,现在我直接发布表单使用,我可以保存我的电子邮件地址,但不能保存密码。

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')
        })
        });

2 个解决方案

#1


0  

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

如果我理解你的问题,你就错过了contentType参数。

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

then try again.

然后再试一次。

#2


0  

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
            };

#1


0  

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

如果我理解你的问题,你就错过了contentType参数。

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

then try again.

然后再试一次。

#2


0  

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
            };