AngularJS获取状态错误详细信息$ http

时间:2017-01-19 19:28:38

标签: javascript angularjs

我是一名新的angularJS学生,我希望在向WebApi发送POST时收到详细信息错误。 我想获取状态500和状态404,并使条件显示反馈而不是状态错误。

我尝试过使用"使用AngularJS"拦截HTTP调用,但每次返回'状态-1'

有人可以帮我吗?

谢谢!

@EDIT

.factory('ajaxFn', function ($http, $cookies, $location, $q) {

    var post = function (url, objeto, data, successFn, errorFn) {
        user = $cookies.get(btoa('user'));
        pass = $cookies.get(btoa('pass'));
        authHeader.Authorization = 'Basic ' + btoa(user + ':' + pass);

        $http({
            method: 'POST',
            url: "//localhost:61115/api/main/" + CompleteUrl,
            headers: authHeader,
            params: objeto,
            data: data
        })
        .then(function (result) {
            successFn(result.data);
        }, function (data) {
            errorFn();
        });
    }
    return {
        post: post
    };
})

这就是我试过的拦截代码

//使用AngularJS拦截HTTP调用。     .config(function($ provide,$ httpProvider){

    // Intercept http calls.
    $provide.factory('MyHttpInterceptor', function ($q) {
        return {
            // On request success
            request: function (config) {
                // console.log(config); // Contains the data about the request before it is sent.

                // Return the config or wrap it in a promise if blank.
                return config || $q.when(config);
            },

            // On request failure
            requestError: function (rejection) {
                // console.log(rejection); // Contains the data about the error on the request.

                // Return the promise rejection.
                return $q.reject(rejection);
            },

            // On response success
            response: function (response) {
                // console.log(response); // Contains the data from the response.

                // Return the response or promise.
                return response || $q.when(response);
            },

            // On response failture
            responseError: function (rejection) {
                // console.log(rejection); // Contains the data about the error.

                // Return the promise rejection.
                return $q.reject(rejection);
            }
        };
    });

    // Add the interceptor to the $httpProvider.
    $httpProvider.interceptors.push('MyHttpInterceptor');

})

0 个答案:

没有答案