FormData
will always be sent as strings. One way to solve the problem is to use JSON. Simply encode your values with JSON.stringify
on the clientside. On serverside you simply decode the values.
Clientside
var fd = new FormData;
var data = {
name: 'john doe',
active: true,
count: 42
};
var prop;
for(prop in data){
fd.append(prop, JSON.stringify(data[prop]));
}
// if you want to upload files, too
fd.append('file', file);
$http({
method: 'post',
url: '/api/upload',
data: fd,
transformRequest: angular.identity,
headers:{ 'Content-Type': undefined }
});
Serverside (PHP, simplified)
$data = [];
foreach($_POST as $prop => $value){
$data[$prop] = json_decode($value);
}
// $data contains the correct values now ..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…