错误请求Angular Node post by id

时间:2016-07-21 13:39:32

标签: angularjs node.js

我的角度代码中有:

 $scope.Update = function(){    
        var data = $.param
        ({
            Problem: $scope.problem
            ,description: $scope.description
            ,resolution: $scope.resolution

        }); 

    console.log(data);
    $http.post("/kbase/" + $scope._id , data).
      success(function(data) { $scope.m_datalist = data; Reset();})
      .error(function(data) {console.log(data + "failure");});    

    }

我在我的节点服务器上有这个:

app.post('/kbase/:m_id', function (req, res) {  
        console.log("here");  
    });

我遇到两个错误:

在客户端:

POST 
XHR 
http://localhost:5000/kbase/578e317bf88c311741b16b66 [HTTP/1.1 400 Bad Request 2ms]

在服务器端:

Error: invalid json
    at Object.exports.error (/Users/ibmtestlab/Documents/workspace/NodeRunJava/node_modules/express/node_modules/connect/lib/utils.js:60:13)
    at IncomingMessage.<anonymous> (/Users/ibmtestlab/Documents/workspace/NodeRunJava/node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)
    at emitNone (events.js:67:13)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:913:12)
    at nextTickCallbackWith2Args (node.js:442:9)
    at process._tickCallback (node.js:356:17)
[90mPOST /kbase/578e317bf88c311741b16b66 [33m400 [90m2ms[0m

我希望至少能见到你好。

思想?

1 个答案:

答案 0 :(得分:1)

您不需要使用$.param

手动序列化数据
var data = {
            Problem: $scope.problem
            ,description: $scope.description
            ,resolution: $scope.resolution

        }; 

$http.post("/kbase/" + $scope._id , data) //pass data as object