user269964 user269964 - 4 months ago 13
Node.js Question

Bad request Angular Node post by id

I have in my angular code:

$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");});

}


I have on my node server this:

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


I'm getting two errors:
On the client side:

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


on the server side:

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


I'm hoping to at least see hello.

Thoughts?

Answer

You don't need to manually serialize data with $.param

var data = {
            Problem: $scope.problem
            ,description: $scope.description
            ,resolution: $scope.resolution

        }; 

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