简体   繁体   English

获取Node.js以从HTML文本字段获取用户输入?

[英]Get Node.js to get user's input from a HTML text field?

I've basically been trying to make a simple game where the user can submit their high-score to a Node.js server. 我基本上一直在尝试制作一个简单的游戏,用户可以将其高分提交给Node.js服务器。 I'm just trying to do something very simple but can't seem to find much to help me online! 我只是想做一些非常简单的事情,但似乎找不到太多帮助我在线的东西!

At the moment I've got the main page to connect and respond to the server using AJAX. 目前,我已经有了使用AJAX连接和响应服务器的主页。

Here's the HTML: 这是HTML:

<div id="test"></div> 
<h3>Upload your high-score:</h3>
Your score: <input type="text" name="yourScore">
<button id="SubmitBtn" type="submit">Submit</button>

Here's the JavaScript which triggers when the "Submit" button is clicked and appends the "test" div when successfully connected (Just to show it responds): 这是JavaScript,当单击“提交”按钮时触发,并在成功连接后附加“测试” div(仅显示其响应):

<script> 
$(document).ready(function() {
$('#SubmitBtn').click(function (event) {
    $.ajax({ 
        url: 'http://localhost', 
        dataType: "jsonp", 
        jsonpCallback: "_testcb", 
        cache: false, 
        timeout: 5000, 
        success: function(data) { 
            $("#test").append(data); 
        }, 
        error: function(jqXHR, textStatus, errorThrown) { 
            alert('Error connecting to the Node.js server... ' + textStatus + " " + errorThrown); 
        } 
    }); 
});

}); });

And here's my Node.js at the moment where it runs and responds: 这是我的Node.js运行并响应的时刻:

var http = require('http'); 
console.log('Game server running...'); 
http.createServer(function (req, res) { 
console.log('Submit button has been clicked.'); 
res.writeHead(200, {'Content-Type': 'text/plain'}); 
res.end('_testcb(\'{"message": "Welcome! Node.js Responding!"}\')'); 
}).listen(80); 

So yeah, if anyone knows how I can get my Node.js server to read the data in the "yourScore" text field and when the "SubmitBtn" button has been clicked to send the data to the server, I would be most thankful! 是的,如果有人知道如何让我的Node.js服务器读取“ yourScore”文本字段中的数据,并且当单击“ SubmitBtn”按钮将数据发送到服务器时,我将非常感谢!

First 第一

You are not sending any data with your AJAX request. 您没有通过AJAX请求发送任何数据。

Possible solution (it is better if you get score input element using an ID or serializing the form element): 可能的解决方案(最好是使用ID获得分数input元素或序列化form元素):

$.ajax({ 
    url: 'http://localhost',
    data: { score : $("input[name='score']").val() },
    dataType: "jsonp", 
    jsonpCallback: "_testcb", 
    cache: false, 
    timeout: 5000, 
    success: function(data) { 
        $("#test").append(data); 
    }, 
    error: function(jqXHR, textStatus, errorThrown) { 
        alert('Error connecting to the Node.js server... ' + textStatus + " " + errorThrown); 
    } 
});

Second 第二

Inside your NodeJS request handler you can get the sent data using the request object. 在NodeJS请求处理程序内部,您可以使用request对象获取发送的数据。

Possible solution: 可能的解决方案:

var http = require('http'); 
http.createServer(function (req, res) {
      var score = req.body.score;
      console.log(score);
      //do whatever you want
      res.writeHead(200, {'Content-Type': 'text/plain'}); 
      res.end('_testcb(\'{"message": "Welcome! Node.js Responding!"}\')'); 
}).listen(80); 

You need to send a POST to the server, check out this simple example 您需要将POST发送到服务器,查看此简单示例

in your script: 在您的脚本中:

$.ajax({
    type: "POST",
    data: {
        yourScore: $('input[name="yourScore"]').val()
    }, 
    //other parameters

and in your server 并在您的服务器中

http.createServer(
   //...
   if (req.method == 'POST'){
      console.log(req.body.yourScore)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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