无法读取节点js中传入请求的数据

unable to read the data of incoming request in node js

本文关键字:请求 数据 读取 节点 js      更新时间:2023-09-26

我正在尝试使用node,express和angularjs创建一个示例登录页面。

下面是我的登录视图

<div class="loginpage">
<div class="loginpage_content">
    <div style="margin-top:30px;padding:10px;width:100%;text-align:center;font-size:20px;font-weight:bold">Please Enter Your Login Crendtials</div>
    <table width="80%" style="margin-top:40px;margin-left:50px;text-align:center;font-size:20px">
        <tr height="40px"><td width="25%">UserName</td><td width="5%">:</td><td width="70%"><input type="text" ng-model="name"></td></tr>
        <tr height="20px"></tr>                     
        <tr height="40px"><td width="25%">Password</td><td width="5%">:</td><td width="70%"><input type="password" ng-model="pass"></td></tr>
        <tr height="30px"></tr> 
        <tr height="40px"><td  colspan="3" style="align:left"><input type="button" value="Login" ng-click="login()"><input type="button" value="Clear" style="margin-left:10px"><a href="" style="margin-left:10px">Change Password</a></td></tr>   
    </table>
    <div style="margin-top:10px;padding:10px;width:99%;text-align:center;font-size:20px;Color:red;display:none">Enter valid Username & Password</div>   
</div>

登录页面控制器

angular.module("Fms").controller('LoginCtrl',['$scope','$http',function($scope,$http)
{
$scope.results ="";
$scope.name ="";
$scope.pass ="";
$scope.login=function()
{
    $http(
    {   
        method: 'get',
        url: '/login',
        data:  'LOG CHECK',
        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    }).
    success(function(response) 
    {
        console.log("success"); // Getting Success Response in Callback
        console.log(response);
    }).
    error(function(response) 
    {
    });
}
  }]);

服务传入请求的代码

  server.get('/login', function(req, res)
  {
         console.log(req.data);
        res.send("received");
  });

问题是我想读取通过get请求传递的数据。我试了很多方法……

console.log (req.data);返回未定义,为什么????

你的代码有两个问题。

1) Angularjs $http get将field作为params发送请求数据。参数值应该是一个对象。

所以发送请求如下

$http(
{   
    method: 'get',
    url: '/login',
    params:  {data:'LOG CHECK'},
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
})

2)在服务器端读取req.query.data而不是req.data的数据