我有一个带有firstname,lastName,zipcode..etc的表单,除了多值输入外,用户还可以选中多个复选框(这将是一个数组)。
我想使用angular js $ http提交此表单,并将它们作为查询字符串传递,目前我有这样的:
的firstName =试验&安培; lastName的=试验&安培; homeZip = 44551&安培; carInterested =本田%2Cford%2Ctoyota
但我的要求是这样的:
的firstName =试验&安培; lastName的=试验&安培; homeZip = 44551&安培; carInterested =本田&安培; carInterested =福特&安培; carInterested =丰田
这是我的代码:
$scope.signUp = function() {
$scope.formData.carInterested= $scope.selection
console.log ($scope.formData);
$http({
method : 'POST',
url : '/sonmeUr/../',
transformRequest: transformRequestAsFormPost,
data : $scope.formData
})
.success(function(data) {
console.log(data);
});
}
在我的服务中我有这个:
.factory( "transformRequestAsFormPost", function() {
function transformRequest( data, getHeaders ) {
var headers = getHeaders();
headers[ "Content-type" ] = "application/x-www-form-urlencoded; charset=utf-8";
return( serializeData( data ) );
}
return( transformRequest );
function serializeData( data ) {
if ( ! angular.isObject( data ) ) {
return( ( data == null ) ? "" : data.toString() );
}
var buffer = [];
for ( var model in data ) {
if ( ! data.hasOwnProperty( model ) ) {
continue;
}
var value = data[ model ];
buffer.push(
encodeURIComponent( model ) +
"=" +
encodeURIComponent( ( value == null ) ? "" : value )
);
}
var source = buffer
.join( "&" )
.replace( /%20/g, "+" )
;
return( source );
}
}
);