Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
what data format your webservice is expecting ? –  Ajay Beniwal Apr 29 '13 at 9:35
    
My web service expects JSON –  axvo Apr 29 '13 at 12:00
add comment

1 Answer 1

up vote 10 down vote accepted

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.

share|improve this answer
1  
Thank for your answer, but $.param doesn't work, an error says it's not defined –  axvo Apr 29 '13 at 12:00
2  
This is a jQuery call. AngularJs doesn't provide a solution out of the box :(. Here is something about it. If you don't use jQuery, here is a snippet to achieve the encoding. I'll update the answer. –  CaioToOn Apr 29 '13 at 12:07
    
it works fine for a simple object, but with an array of objects, it doesn't work. However, I think now it's a server-side problem... –  axvo Apr 29 '13 at 14:08
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.