2016-05-31 6 views
0

以下は私のサーバーのコードのhtmlページでnode.jsサーバーはどのようにしてajaxリクエストのデータにアクセスできますか?

/* GET tone. */ 
router.post('/tone', function(req, res, next) { 
    console.log("what is the body" + req.body.data); 
    tone_analyzer.tone({ text: req.body.data }, function(err, tone) { 
    console.log(req.body.data); 
    if (err) { 
     console.log(err); 
    } else { 
     res.send(JSON.stringify(tone, null, 2)); 
    } 
    console.log(req); 
    }); 
}); 

私のAjaxの呼び出しです。

function toneAnalysis(info){ 
    $.ajax({ 
    url: 'http://localhost:3000/tone', 
    type: 'POST', 
    data: info, 
    success: function(res) { 
     console.log("testing " + info); 
    }, 
    error: function(xhr, status, errorThrown) { 
     console.log(status); 
    } 
    }) 

サーバがreq.body.dataを取得できませんでした。それをログに記録しようとすると、常に定義されていません。誰も私にこれを手伝ってもらえますか?ありがとうございました。

更新: The printed req.body after I used body parser

+0

これは単にreq.bodyである必要があります。ログアウトして、それが定義されているかどうかを確認します。 body-parserを使用する必要があります。 –

+0

2番目のコメントでbody parserが必要な場合もありますが、typeof(req.data)を見れば=== 'string'ならJSONの解析が必要な場合があります。 –

+0

http://expressjs.com/en/4x/api.html#req.body –

答えて

0

リクエストボディは、それはあなたが

let bodyParser = require('body-parser'); 
    const app = express(); 
    app.use(bodyParser.json()); 

router.post('/tone', function(req, res, next) { 
    console.log("what is the body" + req.body); 
    tone_analyzer.tone({ text: req.body}, 
    function(err, tone) { 
    // your code here 
} 
0

を使用することができますJSONであれば、あなたのサーバ設定でこれを持っていますかreq.body

になりますか?

app.use(express.bodyParser()); 

これにより、JSONリクエストを解析できます。あなたがBodyParserを使用することができますし、それをダウンロードし、そのようにNPMを使用してインストールすることができます言及した上記の答えのよう

+0

私のapp.jsにこの行があります –

+0

package.jsonファイルには本体内にボディセイサーがありますか? @endherfang –

+0

@HoussemYahiaouiはい、{ "body-parser": "〜1.13.2"、 "cookie-parser": "〜1.3.5"、 "debug": "〜2.2.0 "、" 012 "、"〜1.11 "、 " express ":"〜4.13.1 "、 " jade ":"〜1.11.0 "、 " morgan ":"〜1.6.1 "、 " serve-favicon ":"〜2.3.0 " } –

1

# npm install bodyparser --save 

は、その後、あなたの$アヤックスコールに戻る、あなたが表され、いくつかのデータを送信していますデータオブジェクトで、そうBodyParserREQ nodejsオブジェクトに別のオブジェクトを追加し、それだからあなたは、単に、送信されたオブジェクトにアクセスすることができBodyParserを使用してあなたはBodyParserを使用して送信されたすべての項目にアクセスしたい場合は、おそらくそうのようにそれをやろうとしてますので、と呼ばれる:あなたはXHRを処理するとき今BodyParserを使用して

const app = require('express')(); 
    let bodyParser = require('body-parser'); 

    // add a new middleware to your application with the help of BodyParser 
    // parse application/x-www-form-urlencoded 
    app.use(bodyParser.urlencoded({ extended: false })); 

    // parse application/json 
    app.use(bodyParser.json()); 

    //Configure the route 
    router.post('/tone', (req, res, next) => { 
    console.log("what is the body" + req.body.data); 
    tone_analyzer.tone({ text: req.body.data}, (err, tone) => { 
     console.log(req.body.data); 
     if (err){ 
      console.log(err); 
     } 
     else{ 
      res.send(JSON.stringify(tone, null, 2)); 
     } 
     console.log(req); 
    }); 
    }); 

、物事は本当に簡単に取得することができますまたはHTTP呼び出し。

関連する問題