Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS服务返回状态_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS服务返回状态

Javascript AngularJS服务返回状态,javascript,angularjs,Javascript,Angularjs,我正在做的自定义$http服务如下所示: angular.factory('myHttp', function($http){ var obj = {}; obj.get = function(path) { return $http.get(path).then(function(result){ return result; },function(result){ console.log("result error:"+path);

我正在做的自定义
$http
服务如下所示:

angular.factory('myHttp', function($http){
  var obj = {};

  obj.get = function(path) {
     return $http.get(path).then(function(result){
        return result;
     },function(result){
       console.log("result error:"+path);
       return result;
   });
 }
    myHttp.get($scope.url).
      then(function(response) {
        console.log("It's success");
        $scope.status = response.status;
        $scope.data = response.data;
      }, function(response) {
        console.log("It's fail");
        $scope.data = response.data || "Request failed";
        $scope.status = response.status;
    });
然后,服务可以这样使用:

angular.factory('myHttp', function($http){
  var obj = {};

  obj.get = function(path) {
     return $http.get(path).then(function(result){
        return result;
     },function(result){
       console.log("result error:"+path);
       return result;
   });
 }
    myHttp.get($scope.url).
      then(function(response) {
        console.log("It's success");
        $scope.status = response.status;
        $scope.data = response.data;
      }, function(response) {
        console.log("It's fail");
        $scope.data = response.data || "Request failed";
        $scope.status = response.status;
    });
它确实会在成功和失败时返回结果。然而,即使它失败了,它仍然会在成功的部分返回。也就是说,如果连接失败,我仍然会在控制台中获得
成功的消息

当连接失败时,如何使其返回失败部分?

您使用的是链接,即除非第一个then处理程序方法返回承诺对象,否则将调用第二个调用成功处理程序

此方法返回一个新的承诺,该承诺通过 successCallback的返回值errorCallback(除非 价值是一种承诺,在这种情况下,它与 使用承诺链在该承诺中解决)。它还通知 通过notifyCallback方法的返回值。承诺是不可能的 无法从notifyCallback方法中解析或拒绝

因为在您的例子中,您是从第一个处理程序返回
结果
对象,所以
then
方法返回的承诺被视为已解决,因此调用第二个
then
的成功处理程序

您可以使用下面的成功/错误处理程序来修复它

obj.get = function (path) {
    return $http.get(path).error(function (result) {
        console.log("result error:" + path);
    });
}

我写了两种方法来获取方法调用和处理错误

**一,$http函数本身已经返回承诺对象

//factory method
obj.get = function (path) {
    return $http.get(path).then(function (results) {
        return results.data;
    });
};

//call from controller
myHttp.get($scope.url).then(function (response) {
    console.log("It's success");
    $scope.status = response.status;
    $scope.data = response.data;
}).error(function (response) {
    console.log("It's fail");
    $scope.data = response.data || "Request failed";
    $scope.status = response.status;
});


2. The Verbose Way
obj.get = function (path) {
    var def = $q.defer();
    $http.get(path).success(function (data) {
        def.resolve(data);
    }).error(function () {
        def.reject("Failed to get");
    });
    return def.promise;
};

//call from controller
myHttp.get($scope.url).then(function (response) {
    console.log("It's success");
    $scope.status = response.status;
    $scope.data = response.data;
},
function (response) {
    console.log("It's fail");
    $scope.data = response.data || "Request failed";
    $scope.status = response.status;
});

以下是清晰理解的链接

感谢您的回答。但是您的工厂方法不会捕获失败连接。这意味着我希望成功和失败都能在返回控制器之前通过工厂进行一些处理。使用详细方式或apply.error(函数(响应)方法进入Factory使用您的详细方式进行了尝试。它可以工作,但无论成功还是失败,都不会传递任何数据。非常感谢您的回答。您的解决方案可以返回错误。但是,如果我希望它在输出为失败之前重试失败连接几次,怎么做?您的解决方案似乎不起作用。