3

I found a strange question in angular $http.

My request code

  $http({
        method: 'GET',
        url: 'http://localhost:8080/server/'
    }).then(function (response) {
      console.log(response.status);
    },function(response){
      console.log(response.status);
    });

Before that, I set the request header

app.factory('myInterceptor', ['$q', function($q) {
        return {
            request: function(config) {
                config.headers['Authorization'] = 'Basic *';
                return config;
            },
            requestError: function(rejectReason) {
                return $q.reject(rejectReason);
            },
            response: function(response) {
                return $q.resolve(response);
            },
            responseError: function(response) {
                console.log(response.status);
                return $q.reject(response);
            }
        };
    }])
    .config(['$httpProvider', function($httpProvider) {
        $httpProvider.interceptors.push('myInterceptor');
    }]);

This page does not exist,so it should return 404,but it returns -1.

If remove this lineconfig.headers['Authorization'] = 'Basic *';,it returns right 404.

Can anybody help me,this is my demo page http://plnkr.co/edit/GhghMNCPcITwXCINISW5?p=preview , thank you very much

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
Dean Chou
  • 31
  • 3
  • 1
    Possible duplicate of [Angular HTTP: Status -1](http://stackoverflow.com/questions/35418688/angular-http-status-1) – Nikhil Bhandari Mar 18 '17 at 17:34
  • Are you sure your server returns a 404? Check the network panel for the status code returned by the server. Most probably the connection timed out, and the server didn't respond with a status code. – Nikhil Bhandari Mar 18 '17 at 17:36
  • What is the URL in the address bar of your browser? – JB Nizet Mar 18 '17 at 17:36
  • 2
    @NikhilBhandari Yes i'm sure,my chrome returns 404,but `console.log` returns -1 in angular code. – Dean Chou Mar 18 '17 at 17:47
  • @JBNizet You can look this http://plnkr.co/edit/GhghMNCPcITwXCINISW5?p=preview, it can reproduce my problem.Thank you. – Dean Chou Mar 18 '17 at 17:50
  • 1
    That doesn't answer my question. This is a simple question, and it matters, because it's probably a CORS problem. Why don't you just answer it? There is no way I can know the answer by looking at your plunkr. Your server probably has CORS enabled, but doesn't authorize the Authorization header. – JB Nizet Mar 18 '17 at 17:51
  • @JBNizet Is right. It is most likely a [CORS problem.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS) – georgeawg Mar 18 '17 at 22:43
  • @JBNizet Thank your.It is really blocked when OPTIONS, but what should I do? – Dean Chou Mar 19 '17 at 03:26
  • I think it's time to close this question.In my case the server set `response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')` in response heasers.Thanks again. – Dean Chou Mar 19 '17 at 03:38

1 Answers1

-1

There are something wrong with your URL over which you are making request.

One can tell by updating the http call backs as follow :

app.controller('MainCtrl', function($scope, $http) {
  $scope.name = 'angular'
  $http({
    method: 'GET',
    url: 'https://cloudzone.eicp.net:8155/server/'
  }).success(function(response) {
    console.log(response.status);
  }).error(function(response) {
    console.log(response.status);
  });
});

You will see that the server is neither returning Success nor Error, it is just getting timeout. Please check the server's availability.

Suggestion: Try some utility such as 'postman' in google chrome and see what results you are getting.

Dhia
  • 10,119
  • 11
  • 58
  • 69
Harsh
  • 11
  • 6
  • The `.success` method does not return status as a property of a response object. The **deprecated** way: `$http(...).success(function onSuccess(data, status, headers, config)`. See [Why are angular $http success/error methods deprecated? Removed from v1.6?](http://stackoverflow.com/a/35331339/5535245) – georgeawg Mar 18 '17 at 22:40
  • @Harsh Thank you for your answer.I keep testing. – Dean Chou Mar 19 '17 at 02:58
  • @georgeawg that is fine, i was suggesting to test the sever's availability; if the URL is actually responding to the requests. – Harsh Mar 20 '17 at 11:57