[英]Receiving an undefined result from an HTTP POST Request in an Angular DataFactory
我是Angular.js的新手,并尝试使用工厂通过http post请求发送数据。 我收到此错误Cannot read property 'success' of undefined
这里是我的代码...
<!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(stud).success ...任何帮助非常感谢...
所以文档说:
var promise = someAsyncMethodCall();
promise.then(function(greeting) {
alert('Success: ' + greeting);
}, function(reason) {
alert('Failed: ' + reason);
});
看到底部,有2个函数传递给then
函数。 所以你的代码是:
DataFactory.setData(stud).then(function(response) {
console.log(response);
}, function (err) {
console.error(err)
});
$ q库是$ http引用的内容。
看到承诺api 。
有这些方法:
然后(successCallback,errorCallback,notifyCallback) - 无论何时或将要解决或拒绝承诺,只要结果可用,就会异步调用其中一个成功或错误回调。 使用单个参数调用回调:结果或拒绝原因。 另外,在解决或拒绝承诺之前,可以将通知回调调用零次或多次以提供进度指示。
此方法返回一个新的promise,它通过successCallback的返回值errorCallback解析或拒绝(除非该值是一个promise,在这种情况下,它使用promise链接解析该值中的值)。 它还通过notifyCallback方法的返回值进行通知。 无法从notifyCallback方法解析或拒绝承诺。
catch(errorCallback) - promise.then的简写(null,errorCallback)
finally(callback,notifyCallback) - 允许您观察承诺的履行或拒绝,但是这样做而不修改最终值。 无论承诺被拒绝还是已解决,这对于释放资源或进行必要的清理都非常有用。 有关更多信息,请参阅完整规范。
因此,为了进一步提高代码,您可以:
DataFactory.setData(stud).then(function(response) {
console.log(response);
})
.catch(function (err) {
console.error(err)
})
.finally(function () {
console.log("Finished the chain");
});
为了完整起见,@ Duncan说了另一个错误,那就是换行符:
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
});
}
}
}]);
return
和$http({
之间的中断$http({
导致错误。所以它需要看起来像这样:
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
});
}
}
}]);
$http
返回一个Promise
。 你需要使用.then()
来注册成功的回调。
更一般的形式是.then(successFn, failFn)
你的问题在函数setData()
:
return
$http({
method: 'POST',
url: 'http://www.w3schools.com/jquery/demo_test_post.asp',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: stud
});
这是两个语句: return
只是从函数返回,然后对$http()
的调用是永远不会达到的附加代码。
小心你在Javascript中放置换行符,移动$http({
到上一行。
哦,正如其他人所说,不推荐使用$http
调用结果的.success
,转而使用普通的promise方法。
它只是降价还是在返回和$ http之间有换行符? 如果你返回那里,$ http就无法访问。 函数可以低于返回值,因为它们是在代码执行之前注册的。
所以你应该写它
return $http({...})
代替
return $http({...})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.