How do I get the HTTP response status code in AngularJS 1.2

前端 未结 8 1792
半阙折子戏
半阙折子戏 2020-12-05 03:04

Using ngResource in AngularJS 1.2rc(x), how do I get the status code now?

RestAPI.save({resource}, {data}, function( response, responseHeaders )         


        
相关标签:
8条回答
  • 2020-12-05 03:32

    I had faced the similar problem.I looked into the angular lib and added a few lines to have status returned in the response itself.In this file,find where promise is being returned.

    Replace code block starting with

    var promise = $http(httpConfig).then(function(response)

    with the following

    var promise = $http(httpConfig).then(function(response) {
                var data = response.data,
                    promise = value.$promise;
    
                if (data) {
                  // Need to convert action.isArray to boolean in case it is undefined
                  // jshint -W018
                  if ( angular.isArray(data) !== (!!action.isArray) ) {
                    throw $resourceMinErr('badcfg', 'Error in resource configuration. Expected ' +
                      'response to contain an {0} but got an {1}',
                      action.isArray?'array':'object', angular.isArray(data)?'array':'object');
                  }
                  // jshint +W018
                  if (action.isArray) {
                    value.length = 0;
                    forEach(data, function(item) {
                      value.push(new Resource(item));
                    });
                  } else {
                    copy(data, value);
                    value.$promise = promise;
                  }
                }
    
                value.status = response.status;
                value.$resolved = true;
    
                response.resource = value;
    
                return response;
              }, function(response) {
                value.status = response.status;
                value.$resolved = true;
    
                (error||noop)(response);
    
                return $q.reject(response);
              });
    

    or you can add this line

    value.status = response.status;
    

    and then access status in code like reponse.status.Though,this is kind of hack but worked for me.I also had to make changes in the minified version.

    0 讨论(0)
  • 2020-12-05 03:35

    I agreed responseHeaders() function will only return response's header,but you can custom it and it's useful anyway.

    1.

    To solve you problem. With the following:($$service is my $resource instance.)

    var serve = new $$service();
    serve.id = "hello_wrongPath"; // wrong path,will return 404
    serve.$get()
        .then(function (data) {
            console.log("~~~hi~~~");
            console.log(data);
            return data;
        })
        .catch(function (error) {
            console.log("~~~error~~~");
            console.log(error);
            console.log(error.status); // --> 404
            console.log(error.statusText); // --> "Not Found"
            console.log(error.config.timeout); // --> 5000
            console.log(error.config.method); // --> GET
            console.log(error.config.url); // --> request url
            console.log(error.headers("content-type"));// --> "text/plain"
            return error.$promise;
        })
        .finally(function(data){
            console.log("~~~finally~~~");
            console.log(data); // --> undefined
        });
    

    In this way,u can only catch status,statusText,timeout,method,headers(same with responseHeaders) in ERROR response.

    2.

    If you want to see response details in success response,I used a interceptor like this:

    ng.module("baseInterceptor", [])
        .factory("baseInterceptor", ["$q", function ($q) {
            return {
                'request': function (config) {
                    console.info(config);
                    //set timeout for all request
                    config.timeout = 5000;
                    return config;
                },
                'requestError': function (rejection) {
                    console.info(rejection);
                    return $q.reject(rejection);
                },
                'response': function (response) {
                    console.log("~~interceptor response success~~");
                    console.log(response);
                    console.log(response.status);
                    console.log(response.config.url);
                    return response;
                },
                'responseError': function (rejection) {
                    console.log("~~interceptor response error~~");
                    console.log(rejection);
                    console.log(rejection.status);
                    return $q.reject(rejection);
                }
            };
        }]);
    

    and then add interceptor to module:

    .config(["$httpProvider", function ($httpProvider) {
        $httpProvider.interceptors.push("baseInterceptor");
    }])
    
    0 讨论(0)
提交回复
热议问题