代码之家  ›  专栏  ›  技术社区  ›  tic

$网址:。从成功获取调用错误函数

  •  4
  • tic  · 技术社区  · 9 年前

    我无法更改端点,如果失败,它将返回具有不同数据的200OK响应。如何使它运行错误函数( then 从承诺中)?

    我的服务:

    self.getData = function() {
        return $http.get('/api/controller/action').then(function(x) {
                //This will be run even on failure. How can I call....
                return x.data;
            }, function(x) {
                //......here, from the front-end, so that, the 2nd function in
                //inside the 'then' in the controller is run.
                //This code will currently never run as it never fails server side.
                return x;
            });
    };
    

    控制器:

    MyService.getData().then(function(x) {
        //Success
    }, function(x) {
        //Failure
    });
    
    2 回复  |  直到 9 年前
        1
  •  7
  •   Phil    9 年前

    使用 $q.reject ,例如

    return $http.get('/api/controller/action').then(function(x) {
        if (x.data === 'some error condition') {
            return $q.reject(x);
        }
    
        2
  •  0
  •   Ayush Mishra    9 年前

    您还可以检查在点击API后得到的响应状态,

    //做点什么

    }