0

Is there any way to configure Angular app to make it available? Im using a factory.

By the way, I'm using localhost as webserver but I'm making a request to other server (same network).

angular.module('demoApp.factories', [])
    .factory('dataFactory', ['$http', function($http) {

    var urlBase = 'external-server:8080';
    var dataFactory = {};
    dataFactory.getTest = function () {
        return $http.get(urlBase + '/test');
    };

    return dataFactory;
}]);
user1214120
  • 173
  • 1
  • 2
  • 15
  • Maybe if I use jQuery for this part instead? – user1214120 Sep 24 '13 at 19:26
  • 1
    http://stackoverflow.com/questions/16661032/http-get-is-not-allowed-by-access-control-allow-origin-but-ajax-is – Jason Sep 24 '13 at 22:58
  • possible duplicate of [AngularJS Error: Cross origin requests are only supported for protocol schemes: http, data, chrome-extension, https](http://stackoverflow.com/questions/27742070/angularjs-error-cross-origin-requests-are-only-supported-for-protocol-schemes) – Chirag May 27 '15 at 02:01

2 Answers2

4

Finally found the solution. Will post here, maybe it can help others:

angular.module('demoApp.factories', [])
    .factory('dataFactory', ['$http', function($http) {

    var urlBase = 'external-server:8080';
    var dataFactory = {};
    dataFactory.getTest = function () {
        //Note the $http method changed and a new parameter is added (callback)
        //the value of the callback parameter can be anything
        return $http.jsonp(urlBase + '/test?callback=myCallback');
    };

    return dataFactory;
}]);

The controller file basically just calling this factory:

angular.module('demoApp.controllers', []).controller('controller1', ['$scope', 'dataFactory', 
        function ($scope, dataFactory) {

    $scope.status;
    $scope.data;
    dataFactory.getTest().success(function (data) 
    {
        $scope.data = data;
    }).error(function (error) 
    {
        $scope.status = 'Unable to load customer data: ' + error.message;
    });
user1214120
  • 173
  • 1
  • 2
  • 15
  • 1
    This will only enable you to do GET requests cross-domain. If you want do to POST,PUT or DELETE for example u'll need to enable CORS in your client and API. – JoakimB Sep 25 '13 at 07:21
1

You may be looking to configure the $httpProvider as specified here: http://better-inter.net/enabling-cors-in-angular-js/

Another way is to create a simple http proxy on your front-end server to make external requests look like to angular that they're coming from the same server.

Michael Tang
  • 4,686
  • 5
  • 25
  • 24