Ajaxcall不发送输入(MongoDB数据库)

时间:2017-05-16 06:58:30

标签: javascript jquery node.js mongodb input

我目前正在与Phaser.js合作开展项目,而且我在使用Highscore时遇到了问题。当游戏进入名为" end"的最后状态时,它会打开一个Bootstrap模态对话框,其中包含已获得的分数和输入,您可以在其中输入您的姓名。当我点击"发送"它应该将两个输入的值放入ajaxcall并将其发送到" /"。但是输入最终是空的,一个console.log(input1 +"" + input2);除了"和"之外什么都没有。我不知道问题是什么,因为我没有收到任何错误。任何帮助表示赞赏。

index.ejs:

var connectionString = 'servicebus_connectionstring';
var serviceBusService = azure.createServiceBusService(connectionString);

var message = {
    body: '',
    customProperties:
    {
        messagenumber: 0
    },
    brokerProperties:
    {
        SessionId: "1"
    }
};

message.body= 'This is Message #101';
serviceBusService.sendTopicMessage('testtopic', message, function(error)
{
    if (error)
    {
        console.log(error);
    }
});

ajaxcalls.js

<div class="col-md-12" id="popup">
    <!-- Modal -->
    <div class="modal fade" id="myModal" role="dialog">
      <div class="modal-dialog">

        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Modal Header</h4>
          </div>
          <div class="modal-body">
            <label for="score">Your Score:</label>
            <input value="" class="form-control" id="score"><br>
            <label for="name">Your Name:</label>
            <input value="" class="form-control" id="name"  placeholder="Choose a name for the leaderboards...">
          </div>
          <div class="modal-footer">
            <button type="button" id="send" class="btn btn-success">Send</button>
            <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
          </div>
        </div>

      </div>
    </div>
  </div>

index.js

var input1 = $('#name').val();
var input2 = $('#score').val();



$('#send').click(function(){
    console.log(input1 + 'x' + input2);
    $.ajax({
        method: "POST",
        url: "/",
        data: {
            Name: input1,
            HScore: input2
        },
        success: function(data){
            console.log('success');
            $('#myModal').modal('hide');
        }
    });
});

mongo.js

router.post('/', function (req, res) {
  var Name = req.body.Name;
  var HScore = req.body.HScore;

  mongoose.model('hs').create(
      {
        player: Name,
        score: HScore
      },
      function (err,player) {
        if(err){
          res.send('Errortext!');
        }
        console.log('POST creating new Player: ' + player);
        res.redirect('/');
      });
});

1 个答案:

答案 0 :(得分:1)

您只需要阅读click侦听器

中的值
$('#send').click(function(){
    var input1 = $('#name').val();
    var input2 = $('#score').val();

    console.log(input1 + 'x' + input2);
    $.ajax({
        method: "POST",
        url: "/",
        data: {
            Name: input1,
            HScore: input2
        },
        success: function(data){
            console.log('success');
            $('#myModal').modal('hide');
        }
    });
});