DiPix DiPix - 1 year ago 89
AngularJS Question

Why I have to send params as an object in $http angular?

I'm making CRUD and if I want to send some data to my backend (node.js) then I receive an error:

angular.js:10765 POST http://localhost:1234/shop/removeProduct/574bf938b16158b40f9c87bc 400 (Bad Request)


$scope.removeProduct = function (partnerId, productId) {
$http.post("/campaign/removeProduct/" + partnerId, productId);

The solution is just simply pack this parameter (
) in an object like this:

$scope.removeProduct = function (partnerId, productId) {
$scope.productData = {productId: productId};
$http.post("/campaign/removeProduct/" + partnerId, $scope.productData);

But why I have to do this like this? By the way, is this correct or should I do it in a different way?

One more thing, how should i refresh data after I added/removed any object?
Is this correct?

$scope.addPartner = function(data) {
({method: 'POST', url: addPartner, data})
(function(response) {
.loadPartnersData(); // this function for refresh data
.alert("Partner added!");
.loadPartnersData = function () {
.get("/campaign/partner-list").then(function(result) {
.partnerList = result.data.partnerList;


router.get('/partner-list', function (req, res) {
Partner.find({}, function (err, partnerList) {
if (err) throw err;

res.json({ partnerList: partnerList });

Answer Source

I'm assuming you want the url to be something like /shop/removeProduct/34523543?productData=5325345. If so then I would use the angular way of declaring $http request:

var url = '/shop/removeProduct/' + partnerId; /* How ever you declare this */
$scope.removeProduct = function() {
  $http({method: 'POST', url, params:{'productData': productId}})
    .then(function(response) {


Angular then takes care of the decoding of the parameters

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download