Node.js 对节点服务器的Dojo Ajax请求

Node.js 对节点服务器的Dojo Ajax请求,node.js,ajax,dojo,Node.js,Ajax,Dojo,我试图使用客户端中的dojo库向节点服务器发出ajax请求。到目前为止,我无法让节点服务器从Dojo请求中提取值。我计划使用这个值,在节点服务器上处理它,然后返回到客户端。我错过了什么 这是我的Node app.js var express = require('express'); var app = express(); app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*");

我试图使用客户端中的dojo库向节点服务器发出ajax请求。到目前为止,我无法让节点服务器从Dojo请求中提取值。我计划使用这个值,在节点服务器上处理它,然后返回到客户端。我错过了什么

这是我的Node app.js

var express = require('express');
var app = express();

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

app.post('/calculate', function(req, res) {
    console.log('Calculation request received!');
    //console.log(req.body);
    //console.log(req.query);
    // Multiply the number by 2
    res.send("?");

});

app.listen(1337, function () {
  console.log('Listening on port 1337');
});
我的dojo+html

<html>
<body>
    <button id='calculate-button'>Calculate</button>
    <!-- load Dojo -->
    <script src="//ajax.googleapis.com/ajax/libs/dojo/1.10.4/dojo/dojo.js"
            data-dojo-config="async: true"></script>

    <script>
    require(["dojo/dom", "dojo/on", "dojo/request", "dojo/domReady!"],
        function(dom, on, request){
            on(dom.byId("calculate-button"), "click", function(evt){
                myValue = 2;
                request.post("http://localhost:1337/calculate", {value: myValue}).then(
                    function(response){
                        console.log("Result: " + response);
                    },
                    function(error){
                    }
                );
            });
        }
    );
    </script>
</body>
</html>
安装npm正文解析器,以便可以在req.body内提取post数据

var app=express之后

添加中间件来初始化它

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
安装和初始化后,发布的数据将作为console.logreq.body提供

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));