When I send a image and text data, have the next problem, this happend when I send data from angular to cakephp 3 with rest service and don’t know how solve:
Possibly unhandled rejection: {
"data":{
"message":"Invalid data type, must be an array or \\ArrayAccess instance.",
"url":"/documents/add",
"code":500,
"file":"C:\\xampp\\htdocs\\frontend_dekma\\app\\vendor\\cakephp\\cakephp\\src\\Utility\\Hash.php",
"line":51
},
"status":500,
"config":{
"method":"POST",
"transformResponse":[null],
"jsonpCallbackParam":"callback",
"headers":{
"Access-Control-Allow-Origin":"*",
"Access-Control-Allow-Headers":
"Accept, Authorization, Cache-Control, Content-Type, X-Requested-With, x-csrf-token",
"Accept":"application/json",
"Authorization":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjEsImV4cCI6MTQ5NzYzNzczNX0.im1g6BeHhMAF-MA0jhzYsU15UAH6BS1tByIqbj2acAQ"},
"url":"/app/documents/add",
"data":{}
},
"statusText":"Internal Server Error"}
Now in angular have my service for send this information:
.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var formData = new FormData();
console.log(file);
// here take the values
formData.append('photo', file.photo);
formData.append('descripcion', file.descripcion);
//show values formData
for (var key of formData.entries()) {
console.log(key[0] + ', ' + key[1]);
}
//post url uploadUrl = '/app/documents/add'
$http.post(uploadUrl, formData, {
transformRequest: angular.identity,
headers: {
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Headers': 'Accept, Authorization, Cache-Control, Content-Type, X-Requested-With, x-csrf-token',
'Content-Type': undefined
}
})
.then(function(response){
console.log(response);
});
}
}])
This is the controller on my backend:
public function add()
{
$document = $this->Documents->newEntity();
$data = ['result' => 'null', 'id' => 'null'];
if ($this->request->is('post'))
{
$document = $this->Documents->patchEntity($document, $this->request->getData());
if ($this->Documents->save($document))
{
$data = ['result' => 'success', 'id' => $document->id];
} else {
$data = ['result' => 'error'];
}
}
$this->set(compact('data'));
$this->set('_serialize', ['data']);
}