gpt4 book ai didi

javascript - 从 Angular DataFactory 中的 HTTP POST 请求接收未定义的结果

转载 作者:行者123 更新时间:2023-12-02 14:50:01 25 4
gpt4 key购买 nike

我是 Angular.js 的新手,尝试使用工厂通过 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(stud).success 行...非常感谢任何帮助...

最佳答案

所以 docs状态:

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 在底层使用的。

请参阅promise api .

有以下方法:

then(successCallback, errorCallback, notifyCallback) – regardless of when the promise was or will be resolved or rejected, then calls one of the success or error callbacks asynchronously as soon as the result is available. The callbacks are called with a single argument: the result or rejection reason. Additionally, the notify callback may be called zero or more times to provide a progress indication, before the promise is resolved or rejected.

This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback (unless that value is a promise, in which case it is resolved with the value which is resolved in that promise using promise chaining). It also notifies via the return value of the notifyCallback method. The promise cannot be resolved or rejected from the notifyCallback method.

catch(errorCallback) – shorthand for promise.then(null, errorCallback)

finally(callback, notifyCallback) – allows you to observe either the fulfillment or rejection of a promise, but to do so without modifying the final value. This is useful to release resources or do some clean-up that needs to be done whether the promise was rejected or resolved. See the full specification for more information.

因此,为了进一步提高您的代码,您可以这样做:

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({ 之间的中断导致错误。因此它需要如下所示:

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
});

}
}

}]);

关于javascript - 从 Angular DataFactory 中的 HTTP POST 请求接收未定义的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36890411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com