23

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?

3
  • 1
    what data format your webservice is expecting ? Commented Apr 29, 2013 at 9:35
  • 1
    My web service expects JSON Commented Apr 29, 2013 at 12:00
  • If you are facing AngularJS specific serialization problem, then httpParamSerializerJQLike is what you need. Take a look at stackoverflow.com/questions/33852190/… Commented Nov 24, 2015 at 4:50

4 Answers 4

34

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank for your answer, but $.param doesn't work, an error says it's not defined
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.
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...
21
angular.toJson(data)

should work in place of

$.param(data)

1 Comment

This should be a comment?
8

fauverism is right, you can use angular.toJson(data). Not instead, but before $.param though.

function TestCtrl($scope, $http){
$scope.postTest = function(){

    var data = [obj1, obj2, obj3];
    var jsonData=angular.toJson(data);
    var objectToSerialize={'object':jsonData};

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

}

Comments

3

you can use $httpParamSerializer or $httpParamSerializerJQLike

$http(
    url: 'myURL',
    method: "POST",
    data: $httpParamSerializer(data),
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.