我是Angular.js的新手,尝试使用工厂通过http post请求发送数据。我收到此错误Cannot read property 'success' of undefined
这里是我的代码...在Angular DataFactory中接收来自HTTP POST请求的未定义结果
<!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>
我正的错误是在线路DataFactory.setData(双头).success ...任何帮助非常感谢......
只是用'。然后(successFn,failFn)'或'。然后(FN).catch (fn)' –
哪里可以使用.then ...你能举一些例子 –