2016-09-15 28 views
0

當我發送Ajax請求來表示我使用req.body.email來捕獲json數據 控制檯中的輸出給我undefined ....請幫助...jquery ajax到Express無法捕獲數據json數據

客戶端

$.ajax({ 
      url: '/getRandomPass', 
      dataType: "json", 

      data:{'emailId':'[email protected]'}, 
      type: 'POST', 
      cache: false, 
      success: function(data) { 
      console.log("cleint recieved data",data); 
      // this.setState({data: data}); 
      }.bind(this), 
      error: function(xhr, status, err) { 
      console.log(xhr,status,err); 
      }.bind(this) 
    }) 

服務器端

app.use(bodyParser.json()); 
    app.post('/getRandomPass', function(req, res) { 
     console.log("req",req.body.emailId); 
     }); 
+0

你設定的響應內容類型? – nikjohn

+0

你的意思是'console.log(「成功」,數據);'輸出html內容? –

+0

是成功的...... – shank

回答

0

您應該使用:

res.set('Content-Type', 'text/plain'); 
res.send(text) 

或:

res.end(text) 
+0

我已經單獨使用兩者來測試事物,但沒有工程 – shank

+0

然後,在代碼的另一部分中必須存在其他問題。 –

+0

可以請你來TeamViewer或Skype,並在我的代碼循環 – shank