Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.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 TypeError:无法读取属性';数据';与拦截器成角度的未定义的_Javascript_Angularjs_Typeerror_Interceptor - Fatal编程技术网

Javascript TypeError:无法读取属性';数据';与拦截器成角度的未定义的

Javascript TypeError:无法读取属性';数据';与拦截器成角度的未定义的,javascript,angularjs,typeerror,interceptor,Javascript,Angularjs,Typeerror,Interceptor,调用RESTful服务时,我有一个拦截器来捕获http错误: services.config(function($httpProvider) { $httpProvider.responseInterceptors.push('globalInterceptor'); var elementsList = $(); // this message will appear for a defined amount of time and then vanish agai

调用RESTful服务时,我有一个拦截器来捕获http错误:

services.config(function($httpProvider) {
    $httpProvider.responseInterceptors.push('globalInterceptor');

    var elementsList = $();

    // this message will appear for a defined amount of time and then vanish again
    var showMessage = function(content, cl, time) {
        $('<div/>')
            .addClass(cl)
            .hide()
            .fadeIn('fast')
            .delay(time)
            .fadeOut('fast', function() { $(this).remove(); })
            .appendTo(elementsList)
            .text(content);
    };
});

services.factory('globalInterceptor', function($q){
    //When the interceptor runs, it is passed a promise object
    return function(promise){

        //In an interceptor, we return another promise created by the .then function.
        return promise.then(function(successResponse){
            //Do your code here if the response was successful
            if (typeof(successResponse) !== 'undefined') {
                if (successResponse.config.method.toUpperCase() != 'GET') {
                    showMessage('Success', 'http-success-message', 5000);
                    return successResponse;
                }
            }
        }, function(errorResponse){
            switch (errorResponse.status) {
                case 400: // if the status is 400 we return the error
                    showMessage(errorResponse.data.message, 'http-error-message', 6000);
                    // if we have found validation error messages we will loop through
                    // and display them
                    if(errorResponse.data.errors.length > 0) {
                        for(var i=0; i<errorResponse.data.errors.length; i++) {
                            showMessage(errorResponse.data.errors[i],
                                'http-error-validation-message', 6000);
                        }
                    }
                    break;
                case 401: // if the status is 401 we return access denied
                    showMessage('Wrong email address or password!',
                        'http-error-message', 6000);
                    break;
                case 403: // if the status is 403 we tell the user that authorization was denied
                    showMessage('You have insufficient privileges to do what you want to do!',
                        'http-error-message', 6000);
                    break;
                case 500: // if the status is 500 we return an internal server error message
                    showMessage('Internal server error: ' + errorResponse.data.message,
                        'http-error-message', 6000);
                    break;
                default: // for all other errors we display a default error message
                    showMessage('Error ' + errorResponse.status + ': ' + errorResponse.data.message,
                        'http-error-message', 6000);
            }

            return $q.reject(errorResponse);
        });
    }
});
我已尝试调试/console.log此“数据”属性,但找不到它。。。 当应用程序加载时,它会得到两个文件OK(200和带有数据属性)和其中两个错误


非常感谢您的任何帮助:-)

我认为您的功能(errorResponse)没有任何问题。 它似乎能正确地沿链传递错误

但是我对您的功能(successResponse)有问题。 只有当两个IF语句得到满足时,它才沿着链向下传递值,在其他情况下,它传递的是未定义的值


我猜您需要在函数(successResponse)的末尾添加$q.reject()

您的服务器在出现400错误时会返回什么?它实际上会返回500!这是REST服务的标准错误(内部服务器错误)。我只在某些异常(数据库错误等)上更改HTTP响应代码。我通过使用拦截器而不是不推荐使用的ResponseInterceptor来解决它。。。
TypeError: Cannot read property 'data' of undefined
    at http://localhost:8080/vendor/angular-1.2.2/angular.js:7479:22
    at deferred.promise.then.wrappedCallback (http://localhost:8080/vendor/angular-1.2.2/angular.js:10655:81)
    at deferred.promise.then.wrappedCallback (http://localhost:8080/vendor/angular-1.2.2/angular.js:10655:81)
    at http://localhost:8080/vendor/angular-1.2.2/angular.js:10741:26
    at Scope.$get.Scope.$eval (http://localhost:8080/vendor/angular-1.2.2/angular.js:11634:28)
    at Scope.$get.Scope.$digest (http://localhost:8080/vendor/angular-1.2.2/angular.js:11479:31)
    at Scope.$get.Scope.$apply (http://localhost:8080/vendor/angular-1.2.2/angular.js:11740:24)
    at done (http://localhost:8080/vendor/angular-1.2.2/angular.js:7744:45)
    at completeRequest (http://localhost:8080/vendor/angular-1.2.2/angular.js:7918:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://localhost:8080/vendor/angular-1.2.2/angular.js:7874:11) angular.js:9159