【发布时间】:2019-12-01 20:53:36
【问题描述】:
我正在尝试使用 $http.get 调用端点并检查成功代码是否为 200,然后使用响应数据,否则我需要调用其他端点。我试图检查调用是成功还是错误,如下所示,
$scope.getRequest = function () {
var url = $rootScope.BaseURL;
var config = {
headers: {
'Authorization': `Basic ${$scope.key}`,
'Prefer': 'odata.maxpagesize=10000'
}
};
$http.get(url, config)
.success(
function (response) { // success async
$scope.viewRequest.data = response.data;
})
.error(function (response) { // failure async
var url = $rootScope.diffURL;
$http.get(url, config)
.success(
function (response) { // success async
$scope.viewRequest.data = response.data;
})
.error(function (response) { // failure async
console.log("There was an error getting the request from CORE");
});
});
};
我希望如果对 $scope.BaseURL 的调用失败,它将转到错误函数并调用 $scope.diffURLreturns 响应,但我遇到错误
angular.js:14800 TypeError: $http.get(...).success 不是函数
GET https:\\example.com\... 400 (Bad Request)可能未处理的拒绝:
{"data":{"error":{"code":"1001","message":"用于查询表达式的属性未在类型 'T' 中定义。"}},"status":400,"config":{"method":"GET","transformRequest":[null]," transformResponse":[null],"jsonpCallbackParam":"callback","headers":{"Authorization":"Basic 0h","Prefer":"odata.maxpagesize=10000","Accept":"application/json, text/plain, /"},"url":"https://example.com...,"statusText":"Bad Request","xhrStatus":"complete"}`
【问题讨论】:
-
错字:
.sucess不是.success。 -
.success 在 1.3 之前有效,如果你的 Angular js 版本高于 1.3,请使用 then()
-
@NagaSaiA 我尝试使用 .then 但我不确定如何检查呼叫是否成功,以便我可以呼叫另一个端点。你能告诉我如何使用 .then
-
@Amy 谢谢。但即使成功,我也会遇到同样的错误。
-
我不希望修复一个缺陷来解决代码中的所有缺陷,尤其是 400 BAD REQUEST。
标签: javascript angularjs angularjs-http