I am trying to send a post request with both a header and request body. So far I have reached this point:
createJob: function(jobName, jobAddress, jobContact, jobComments) {
var payload = {
name: jobName,
address: jobAddress,
contact: jobContact,
comments: jobComments
};
console.log(payload);
return $resource(route, {}, {
save: {
method: 'POST',
header: {'Content-Type': 'application/json'},
transformRequest: function(data){
console.log('Data in transform request is');
console.log(data);
return data; // this will go in the body request
}
}
});
}
I am not sure where to place the payload in this case, any help? Also when making the call, I am currently trying to do something like:
createJob(this.jobName, this.jobAddress, this.jobContact, this.jobComments).
save().$promise.
then(function (response) {
console.log('Create job response is');
console.log(response);
}).
catch(function (error) {
console.log('Create job error is');
console.log(error);
});
Any help would be appreciated!
I came to a solution for anyone that is interested:
createJob: function(jobName, jobAddress, jobContact, jobComments) {
var payload = {
name: jobName,
address: jobAddress,
contact: jobContact,
comments: jobComments
};
console.log(payload);
return $resource(route, {}, {
save: {
method: 'POST',
transformRequest: function(data){
console.log('Data in transform request is');
console.log(data);
return angular.toJson(data); // this will go in the body request
}
}
}).save({}, payload);
}
createJob(this.jobName, this.jobAddress, this.jobContact, this.jobComments).$promise.
then(function (response) {
console.log('Create job response is');
console.log(response);
//Refresh the page so newly created job can be seen
window.location.reload();
}).
catch(function (error) {
console.log('Create job error is');
console.log(error);
});