-1

I'm new to angularJS (started to learn it yesterday). I'm trying to get data from a server. The curl command works perfectly:

curl -i 175.36.8.90:5000/todo/api/v1.0/tasks

(It will not work for you since this is not the real IP of the server).

How can I get the same thing using angular?

I couldn't find anything - maybe because I'm so new to the subject that I don't even know to search for the right terms.

Binyamin Even
  • 3,318
  • 1
  • 18
  • 45

1 Answers1

1

I'm using this piece of code and works perfectly for me:

var MyApp = angular.module('MyApp'); 
MyApp.factory('DB_Services', ['$http' , '$q' , function($http , $q) {
    var l_Result ;
    var DB_Services = function(p_URL_Root , p_Query) {
        var l_deferred = $q.defer();
        var l_params   = JSON.stringify(p_Query) ;
        var l_url      = "http://localhost:8080/BLABLA/BLABLA_2";
        var req = { url    : l_url, 
                    method :"GET", 
                    timeout:10000 , 
                    headers: { 
                        'Content-Type': 'application/json ; charset=UTF-8'
                    }, 
                   params:{request:p_Query}
                  } ;
        $http(req ).
                    success(function(data, status, headers, config) {
                         l_deferred.resolve({Server_Response:data , Server_Status: status});
                    }).
                    error(function(data, status, headers, config) {
                         l_deferred.resolve(status);
                    });
                    return l_deferred.promise;
        return l_deferred.promise;
    } ;

    return DB_Services;

}]);

EDIT:

I should add that this code approaches a SERVLET who in its turn accessed the DB (I'm guessing that you do not intend to have direct access from the client to the DB, right?).

FDavidov
  • 3,505
  • 6
  • 23
  • 59
  • Don't use [deffered promise anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it)! ... `$http` already returns a promise. Also `success` and `error` are deprecated. Also ContentType header makes no sense on `GET` ..there is no request content – charlietfl Jun 05 '17 at 13:01
  • Dear @charlietfl, I guess that you and me bring different experiences... You obviously spend most of your time developing in JavaScript + AngularJS and hence have so deep knowledge of it (and, have the time and motivation to read the manual from A to Z). Me, I spend part of my time managing other developers and part doing my own development in a number of languages at the same time, so obviously cannot achieve the same level of proficiency in AngularJS as yours. That is why this forum exists, to have experts like you helping others like me. Cheers!! – FDavidov Jun 05 '17 at 13:05
  • Use `then()` ... `return $http.get(url, {params: ..).then(function(resp){ return resp.data;})` – charlietfl Jun 05 '17 at 13:22