无法读取节点js中传入请求的数据
unable to read the data of incoming request in node js
我正在尝试使用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
的数据
相关文章:
- node.js请求数据事件未在CORS ajax调用中触发
- 一台特定计算机的Ajax请求数据未定义/失败
- 如何在具有某些 UI 视图的状态中管理工厂单个请求数据
- Hapi-js重定向到另一个带有额外请求数据的路由
- 将所有angular2捆绑包包含在index.html中会使导入从内存获取数据,而不是再次从服务器请求数据
- jQuery 和 Ajax 请求数据问题
- Symfony2将ajax请求数据传递给表单呈现控制器
- JQuery ajax请求数据
- 如何在浏览器中压缩json请求数据
- 点击按钮从服务器nodejs请求数据
- 在zapier中发送请求数据时出现空指针异常
- 如何显示弹出窗口以在使用纯 JavaScript 从 Web 服务器获取/请求数据时禁用搜索按钮
- 如何发出请求数据的提示
- 每x秒从服务器请求数据的最佳方法
- 在页面上存储 Ajax 请求数据的最佳方式
- 如何在 d3 中从 json 请求数据
- 在 chrome 调试器中更改 XHR 请求数据
- 使用 jQuery 从 NodeJS 服务器请求数据
- 节点.JS请求数据长度小于内容长度中给出的数据长度
- 过滤列表并通过 ajax 请求数据