Angular JS POST request not sending JSON data
If you are serializing your data object, it will not be a proper json object. Take what you have, and just wrap the data object in a JSON.stringify()
.
$http({ url: '/user_to_itsr', method: "POST", data: JSON.stringify({application:app, from:d1, to:d2}), headers: {'Content-Type': 'application/json'}}).success(function (data, status, headers, config) { $scope.users = data.users; // assign $scope.persons here as promise is resolved here }).error(function (data, status, headers, config) { $scope.status = status + ' ' + headers;});
I have tried your example and it works just fine:
var app = 'AirFare';var d1 = new Date();var d2 = new Date();$http({ url: '/api/test', method: 'POST', headers: { 'Content-Type': 'application/json' }, data: {application: app, from: d1, to: d2}});
Output:
Content-Length:91Content-Type:application/jsonHost:localhost:1234Origin:http://localhost:1234Referer:http://localhost:1234/index.htmlUser-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36X-Requested-With:XMLHttpRequestRequest Payload{"application":"AirFare","from":"2013-10-10T11:47:50.681Z","to":"2013-10-10T11:47:50.681Z"}
Are you using the latest version of AngularJS?
You can use FormData API https://developer.mozilla.org/en-US/docs/Web/API/FormData
var data = new FormData;data.append('from', from);data.append('to', to);$http({ url: '/path', method: 'POST', data: data, transformRequest: false, headers: { 'Content-Type': undefined }})
This solution from http://uncorkedstudios.com/blog/multipartformdata-file-upload-with-angularjs