【问题标题】:Send JSON from angular.js to node.js将 JSON 从 angular.js 发送到 node.js
【发布时间】:2018-06-22 15:54:09
【问题描述】:

如果我将 JSON 从前端 angular.js 发送到后端 node.js,它会给我一个错误:TypeError: Cannot read property username of undefined。我也在使用 Express。

前端 (Angular.js)

var username_data = $scope.LoginUsername;
var password_data = $scope.LoginPassword;
var data = {username : username_data, password : password_data};

$http.post('/api/login', data)
.then(function (res) {
    //to do...
});

带有 Restful API 的后端 (node.js):

app.post('/api/login', function(req, res) {
    console.log(req.data.username);
    console.log(req.data.password);
});

【问题讨论】:

    标签: javascript jquery angularjs json node.js


    【解决方案1】:

    表示数据不存在,试试req.body吧

    app.post('/api/login', function(req, res) {
        console.log(req.body.username);
        console.log(req.body.password);
    });
    

    【讨论】:

    • console.log 返回undefined
    • 你能把 console.log(req) 显示的内容贴出来吗;
    • __onFinished: { [Function: listener] queue: [Array] }, [Symbol(outHeadersKey)]:​​ { 'x-powered-by': [Array], 变化:[Array] } } , _startAt: [12994, 141275677], _startTime: 2018-01-13T10:30:26.601Z, _remoteAddress: '::ffff:127.0.0.1', 正文: { 用户名: 'cccccc', 密码: 'dddd' }, _body: true, length: undefined, read: [Function], originalMethod: 'POST', route: Route { path: '/api/login', stack: [ [Object] ], methods: { post: true } } } (节点:5072)[DEP0013] DeprecationWarning:不推荐调用没有回调的异步函数。
    • 对不起,如果我第一次不清楚,是从手机发帖
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 2018-01-09
    相关资源
    最近更新 更多