Skip to content

Instantly share code, notes, and snippets.

@eduardoromero
Last active August 29, 2015 14:26
Show Gist options
  • Save eduardoromero/d480ef1604f9d7ecf1f2 to your computer and use it in GitHub Desktop.
Save eduardoromero/d480ef1604f9d7ecf1f2 to your computer and use it in GitHub Desktop.
Post to a Cake Controller with $this->data ala Cake.
$scope.enviar = function() {
if($('#contactar form').form('is valid')) {
$('#contactar form').addClass('loading');
var url = app_backend + '/contactar/post/' + id + '/post.json';
var data = {};
angular.forEach($scope.Contacto, function(field, value) {
console.log(field, value);
if(angular.isUndefined(value)) {
value = '';
}
fname = 'data[Contacto][' + field +']';
data[fname] = value;
});
$http({
method: 'PUT',
url: url,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
data: data
}).success(function(response, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
if(response && angular.isDefined(response)) {
$('#contactar form').modal('hide');
}
$('#contactar form').removeClass('loading');
}).error(function(response, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
//console.log('--- Error posting Company, response:');
//console.log(response);
$('#contactar form').removeClass('loading');
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment