2017-06-26 6 views
0

クライアントのDojoライブラリーを使用してノード・サーバーに対してajax要求を作成しようとしています。これまでのところ、NodeサーバーはDojoの要求から値を抽出することはできません。この値をNodeサーバーで処理してクライアントに返す予定です。私は何が欠けていますか?ここでノード・サーバーへのDojo Ajax要求

はあなたが後にreq.body

npm install body-parser --save 

内のポストデータを抽出できるようにするため、私のノード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'); 
}); 

私の道場+ 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> 

答えて

1

は、NPM body-parserをインストールしていますvar app = express();

var bodyParser = require('body-parser'); 

データがconsole.log(req.body)

Official Doc

として利用できるようになります投稿、

app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 

した後インストールし、init、それを初期化するためのミドルウェアを追加

関連する問題