angularjs $ http总是返回-1,响应状态为404

时间:2017-03-18 17:21:25

标签: angularjs cors angular-http

我在角$ http发现了一个奇怪的问题。

我的请求代码

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

在此之前,我设置了请求标题

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');
    }]);

此页面不存在,因此它应返回404,但它返回-1。

如果删除此行config.headers['Authorization'] = 'Basic *';,则返回右侧404。

任何人都可以帮助我,这是我的演示页面http://plnkr.co/edit/GhghMNCPcITwXCINISW5?p=preview,非常感谢

1 个答案:

答案 0 :(得分:-1)

您提出请求的网址存在问题。

可以通过更新http回叫来判断如下:

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);
  });
});

您将看到服务器既没有返回成功也没有错误,它只是超时。 请检查服务器的可用性。

建议:在谷歌浏览器中尝试一些实用程序,例如“postman”,看看你得到了什么结果。

相关问题