admin管理员组

文章数量:1332890

I'm trying to create a simple app using Angular that will consume my API. I'm using a VM to run the code, and I access it on my puter, so to call the API from my machine I can use cURL or any other HTTP client and everything works. An example:

curl -k --user [email protected]:password

And that would return a list of travelers for example. I need to "trust" the certificate as it is not valid. So on the browser at first the call would return net::ERR_INSECURE_RESPONSE, so I'm just going to the API URL and add the exception and now I don't have this issue anymore. Then I had to add basic authentication, and it seems to work. Let's see what is my code and please let me know if you see anything wrong, I'm following this tutorial that consume an external API:

app.js:

angular.module('TravelerApp', [
    'TravelerApp.controllers',
    'TravelerApp.services'
]);

services.js:

angular.module('TravelerApp.services', [])
.factory('TravelerAPIService', function($http) {
    var travelerAPI = {};

    $http.defaults.headersmon.Authorization = 'Basic ABC743HFEd...=';

    travelerAPI.getTravelers = function() {
        return $http({
            method: 'GET',
            url: ''
        });
    }

    return travelerAPI;
});

Finally, the controllers.js:

angular.module('TravelerApp.controllers', [])
.controller('travelersController', function($scope, TravelerAPIService) {
    $scope.travelersList = [];

    TravelerAPIService.getTravelers()
    .success(function(data) {
        console.log('SUCCESS');
        $scope.travelersList = data;
    })
    .error(function(data, status) {
        console.log('ERROR');
        $scope.data = data || "Request failed";
        $scope.status = status;
    });
});

The error status code is 0, and the error data is an empty string.

I'm trying to create a simple app using Angular that will consume my API. I'm using a VM to run the code, and I access it on my puter, so to call the API from my machine I can use cURL or any other HTTP client and everything works. An example:

curl -k --user [email protected]:password https://api.my.domain./v1/traveler/get

And that would return a list of travelers for example. I need to "trust" the certificate as it is not valid. So on the browser at first the call would return net::ERR_INSECURE_RESPONSE, so I'm just going to the API URL and add the exception and now I don't have this issue anymore. Then I had to add basic authentication, and it seems to work. Let's see what is my code and please let me know if you see anything wrong, I'm following this tutorial that consume an external API: http://www.toptal./angular-js/a-step-by-step-guide-to-your-first-angularjs-app

app.js:

angular.module('TravelerApp', [
    'TravelerApp.controllers',
    'TravelerApp.services'
]);

services.js:

angular.module('TravelerApp.services', [])
.factory('TravelerAPIService', function($http) {
    var travelerAPI = {};

    $http.defaults.headers.mon.Authorization = 'Basic ABC743HFEd...=';

    travelerAPI.getTravelers = function() {
        return $http({
            method: 'GET',
            url: 'https://api.my.domain./v1/traveler/get'
        });
    }

    return travelerAPI;
});

Finally, the controllers.js:

angular.module('TravelerApp.controllers', [])
.controller('travelersController', function($scope, TravelerAPIService) {
    $scope.travelersList = [];

    TravelerAPIService.getTravelers()
    .success(function(data) {
        console.log('SUCCESS');
        $scope.travelersList = data;
    })
    .error(function(data, status) {
        console.log('ERROR');
        $scope.data = data || "Request failed";
        $scope.status = status;
    });
});

The error status code is 0, and the error data is an empty string.

Share Improve this question asked Mar 26, 2014 at 17:55 DachmtDachmt 2,0994 gold badges29 silver badges45 bronze badges 1
  • I get a similar result when I kill the HTTP server and make a request. Maybe related: stackoverflow./questions/2000609/jquery-ajax-status-code-0 – gak Commented Apr 8, 2014 at 22:40
Add a ment  | 

2 Answers 2

Reset to default 4

Precisions:

I have the same behavior with an HTTP POST query. I am sure :

  • no request have been made on the server
  • it's angular that don't sent the query

And finally I find the answer:

Since I (and probably you) are sending on a self signed httpS server. Chrome flag it as none safe.

I fix this issue by putting the address on my browser and manually accept the certificate.

Probably related : XMLHttpRequest to a HTTPS URL with a self-signed certificate

I would suggest to use Trusted CA Signed SSL Certificate rather then Self-Signed Certificates which would solve your problem as most browsers do not accept the self signed certificates like Google Chrome,Firefox,etc.

本文标签: javascriptAngularJS API call error with http GETStack Overflow