2016-09-15 57 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