Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/413.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从Javascript$.post接收未定义的内容_Javascript_Node.js_Express_Post - Fatal编程技术网

从Javascript$.post接收未定义的内容

从Javascript$.post接收未定义的内容,javascript,node.js,express,post,Javascript,Node.js,Express,Post,我正在编写一个web应用程序,它允许我从控制面板控制lgWebOS智能电视。我用Node.js和Express设置了一个服务器,当为“/”请求时,它将返回一个带有输入区域的基本HTML文件以更改卷。我希望能够将文本/输入框中的数据发送回我的服务器,以传递到lgTV。代码: HTML: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewp

我正在编写一个web应用程序,它允许我从控制面板控制lgWebOS智能电视。我用Node.js和Express设置了一个服务器,当为“/”请求时,它将返回一个带有输入区域的基本HTML文件以更改卷。我希望能够将文本/输入框中的数据发送回我的服务器,以传递到lgTV。代码:

HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximux-scale=1, user-scalable=no">
    <title>lgWebOS App</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <style type="text/stylesheet">
      h1{
        font-family: fantasy;
        color: black;
      }
    </style>
    <script type="text/javascript">
      $(document).ready(function(){
        var vol;
        $('#volSubmit').on('click', function(){
          vol = $("#volVal").val();
          $.post("http://localhost:3000/volume",
          {volVal: vol}, function(data){
            if(data==='done'){
              alert('Volume Changed');
            }
          });
        });
      });
    </script>
  </head>
  <body>
    <div class="container-fluid">
      <div class="container col-md-12">
        <h1 id="title" style="text-align:center;">LGwebOS Application</h1>
      </div>
      <div class="volume col-md-6">
        <h3>Set Volume Here</h3>
        <input type="text" id="volVal" value="3">
        <input type="button" id="volSubmit" value="Set">
      </div>
    </div>
  </body>
</html>
module.exports = {
  config: expressConfig,
  getHome: getHome,
  post: volPost,
  start: runServer
}

var lgtv = require('./webOS.js')
var bodyParser = require('body-parser');
var express = require('express');
var app = express();

function expressConfig(){
  lgtv.config();
  app.use(bodyParser.json());
  app.use(function(req, res, next){
    res.header('Cache-Control', 'private, no-cache, no-store, must-revalidate');
    res.header('Expires', '-1');
    res.header('Pragma', 'no-cache');
    next();
  });
  app.set('views', __dirname = './views');
  app.set('views engine', 'ejs');
  app.engine('html', require('ejs').renderFile);
}

function getHome(){
  app.get('/', function(req, res){
    res.render('index.html');
  });
}

function volPost(){
  app.post('/volume', function(req, res){
    console.log(req.body);
    var level = req.body.volVal;
    console.log(level);
    lgtv.setVol(level);
  });
}

function runServer(){
  var server = app.listen(3000, function(err){
    if(err){
      console.log('An error occred: ' + err);
    }
    console.log('Web server is running on localhost port 3000');
  });
}

在服务器文件中,在我的volPost函数中,我将其设置为记录变量“level”和请求主体。变量级别应该是从HTML发送的数据,但它返回为未定义,请求主体返回为空JS对象。

您不返回任何内容。…@epascarello我更改了标题,这更有意义吗?是的,有意义。您应该返回响应,以便客户端显示
警报
。另外,
expressConfig
应该在
volPost
之前定义。在节点中的
volPost
上,您需要发送一个响应,类似于
response.send('done')
。否则,浏览器将永远不会知道服务器上发生了什么