I just started learning Angular.js and I'm experimenting with using a factory to send data through an http post request. However, I encountered an error that says
Cannot read property 'success' of undefined
. Here is the code snippet in question...
<!DOCTYPE html>
<html ng-app="myApp">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>
<div ng-controller="myCtrl">
</div>
<script>
var app = angular.module('myApp', []);
app.factory('DataFactory', ['$http', function($http) {
return {
setData: function(stud) {
return
$http({
method: 'POST',
url: 'http://www.w3schools.com/jquery/demo_test_post.asp',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: stud
});
}
}
}]);
app.controller('myCtrl', function($scope, DataFactory) {
var stud = {
name: "Alex",
city: "Berlin"
};
DataFactory.setData(stud).then(function(response) {
console.log(response);
})
.catch(function(err) {
console.error(err)
})
.finally(function() {
console.log("Finished the chain");
});
});
</script>
</body>
</html>
The specific error appears at line where it says DataFactory.setData(stud).success... any assistance would be greatly appreciated...