从Angular $ http POST传递数据数组

时间:2022-06-11 21:36:44

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

我需要将一个对象数组从我的Angular应用程序传递给一个带有Nancy框架的.Net Web服务。

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.

但服务器发送500内部服务器错误。我不知道为什么它不起作用。我不是专家的网络服务专家,但我认为这是一个序列化问题。

Can someone help me?

有人能帮我吗?

4 个解决方案

#1


33  

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:

根据这篇文章,你是对的,这是关于序列化。 Angular不会为您自动序列化数据,您需要在发送数据之前解析数据:

...

$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.

如果你不使用jQuery,你需要自己推送$ .parse。这里有一个片段或者您可以调整jQuery实现。

#2


18  

angular.toJson(data)

should work in place of

应该代替

$.param(data)

#3


8  

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

fauverism是对的,你可以使用angular.toJson(数据)。不是,而是在$ .param之前。

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

}

}

#4


2  

you can use $httpParamSerializer or $httpParamSerializerJQLike

你可以使用$ httpParamSerializer或$ httpParamSerializerJQLike

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

#1


33  

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:

根据这篇文章,你是对的,这是关于序列化。 Angular不会为您自动序列化数据,您需要在发送数据之前解析数据:

...

$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.

如果你不使用jQuery,你需要自己推送$ .parse。这里有一个片段或者您可以调整jQuery实现。

#2


18  

angular.toJson(data)

should work in place of

应该代替

$.param(data)

#3


8  

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

fauverism是对的,你可以使用angular.toJson(数据)。不是,而是在$ .param之前。

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

}

}

#4


2  

you can use $httpParamSerializer or $httpParamSerializerJQLike

你可以使用$ httpParamSerializer或$ httpParamSerializerJQLike

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