Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array of data from Angular $http POST

Tags:

I need to pass an array of object from my Angular application to a .Net web service with Nancy framework.

I tried this :

function TestCtrl($scope, $http){     $scope.postTest = function(){          var data = [obj1, obj2, obj3];          $http({             url: 'myURL',             method: "POST",             data: data,             headers: {                      'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'             }         }).success(function(data){             alert("done");         });     } } 

But server send 500 Internal server error.
I don't know why it doesn't work. I'm not an expert web service expert but I think it's a serialization problem.

Can someone help me?

like image 926
axvo Avatar asked Apr 29 '13 09:04

axvo


1 Answers

According to this post, you're right, this is about serialization. Angular doesn't automatic serialize the data for you, you need to parse the data before sending it:

...  $http({   url: 'myURL',   method: "POST",   data: $.param(data),   headers: {     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'   } })... 

If you don't use jQuery, you'll need to roll your own $.parse. There is a snippet here or you could adapt jQuery implementation.

like image 166
Caio Cunha Avatar answered Sep 30 '22 06:09

Caio Cunha