0

There is a Webservice that is returning a json object in below format:

["Brazil","Argentina","Chile"]

(this json object has been parsed by Online JSON parsers, which proves it is a proper json object)

In AngularJS, I wrote the below code to consume the WebService.

app.factory('webServiceFactory', ['$http', function ($http) {
    return $http.get('http://SomeWebService/api/anOperation')
            .success(function (data) {
                return data;
            })
            .error(function (err) {
                return err;
            });
}]);

When I call this in my controller, the success part is never hit and, instead, the error part is executed. That means there is some error calling the web-service. I think it has to do something with the JSON object that is being returned.

Edit 1: The Controller code

app.controller('mainController', ['$scope', 'webServiceFactory', function ($scope, webServiceFactory) {

webServiceFactory.success(function (data) {
    $scope.Countries = data;
});

}]);

Edit 2: Localhost & CORS

When I hosted the service in my local IIS and consumed it using localhost, it worked. That means there is no error in consuming the JSON String array.

However, when I use a Fully qualified name or machine name, the FireFox indicates CORS header missing 'Access-Control-Allow-Origin'.

This might lead to another question, as to how to make it CORS compatible. However, the main question is closed.

So, my question, is How do I consume a web-service in AngularJS that returns just an array of strings with no Key/Value pair?

Thanks

2
  • return in success does nothing. Show how you are using this in controller. Also inspect the actual request in browser dev tools network for clues Commented Oct 22, 2015 at 22:29
  • Hey using Chrome's or Firefox's developer tools check if your response's Content-Type header is application/json. Commented Oct 22, 2015 at 22:45

2 Answers 2

1

maybe you are doing a request to a service in other domain; that is not possible since exists the "same origin policy" https://www.w3.org/Security/wiki/Same_Origin_Policy

1
  • Thanks @Johan, I think that's the issue. I need to figure out a way to make it CORS compatible. Commented Oct 23, 2015 at 16:28
1
     app.factory('webServiceFactory', ['$http', function ($http) {
            return
             {
               getCountries: function(callback){
                   $http.get('http://SomeWebService/api/anOperation').then(function(reponse){
                     callback(response.data);
                 });
               }
             };
            }]);

Then in your controller

webServiceFactory.getCountries(function(countries){
  $scope.countries = countries;
});
5
  • You should use then() instead success(). Commented Oct 22, 2015 at 22:53
  • When tried this, I am getting this error: "Error: [$injector:undef] Provider 'webServiceFactory' must return a value from $get factory method. errors.angularjs.org/1.4.7/$injector/undef?p0=webServiceFactory Commented Oct 23, 2015 at 4:45
  • I updated the response, the problem was the "=" instead of ":" in the factory. Moreover ".then(..)" is the way to go as said previously. Commented Oct 23, 2015 at 12:48
  • It did not work even after the correction. However the error that was reported in FireFox was different. Something related to CORS. When I am using Localhost, it works. Updating the main Question. Thanks for the ".then()". Commented Oct 23, 2015 at 16:17
  • Ok, your api server just needs to allow the url of your website hosting angular since you are doing cross domain request (cors) It should be pretty easy now that you have the problem well cornered. Commented Oct 23, 2015 at 21:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.