1

I have searched it a lot , in all cases , they usually want to show it on HTML directly , but my purpose is to use the data in Angular JS only.

angular.module('myApp').factory('UserService', ['$http', '$q', function($http, $q){

    var url ;
    $http.get('/HomeAccessService-1/static/js/data.json').then(function(response) {
        url = response.data;
        //alert(JSON.stringify(url.data));
        });
    //var REST_SERVICE_URI = 'http://localhost:8080/HomeAccessService-1/user/';
    var REST_SERVICE_URI = JSON.stringify(url.data);
    var factory = {
        fetchAllUsers: fetchAllUsers,
        createUser: createUser,
        updateUser:updateUser,
        deleteUser:deleteUser
    };

return factory;

    function fetchAllUsers() {
        var deferred = $q.defer();
        $http.get(REST_SERVICE_URI)
            .then(
            function (response) {
                deferred.resolve(response.data);
            },
            function(errResponse){
                console.error('Error while fetching Users');
                deferred.reject(errResponse);
            }
        );
        return deferred.promise;
    }

I want to use this url data in other methods , the data is actually a REST API URL which i put in data.json file

{"data":"http://someotherip:8080/Service/user/"}

It shows object Object when alert ,

I dont want to show it in HTML but to use the data in angular js methods.

4
  • try to alert inside function. Commented Mar 12, 2017 at 6:46
  • i dont want to alert , i am using alert just to see the value , the aim is to use the url value , which is coming object Object Commented Mar 12, 2017 at 6:50
  • store the response in $scope variable and use it anywhere within the JS . $scope.url = response.data; Commented Mar 12, 2017 at 7:03
  • angular js a worst language ever Commented Mar 12, 2017 at 7:15

4 Answers 4

1

Alert is showing up before your response reach. Put alert inside request promise. You will get the response. and use json.stringify(response.data) to make a json string

Sign up to request clarification or add additional context in comments.

2 Comments

when i use var REST_SERVICE_URI = JSON.stringify(url.data); outside the http call , i get error in UI - Cannot read property 'data' of undefined
$http.get("./data/data.json").then(function(response){ var data= response; console.log(data); });
0

You should call the alert inside the promise otherwise it will be undefined when you alert it,

  $http.get('/Service-1/static/js/data.json').then(function(response) {
        url = response.data;
        alert(JSON.stringify(url.data));
   });*/

Inorder to display the correct data, you should use JSON.stringify()

alert(JSON.stringify(url.data));

8 Comments

@anshulkatta use JSON.stringify as mentioned in the answer
ok it works , but how do i pass this to another variable let say var REST_SERVICE_URI and use it in other methods inside that same angular js file ,
define a angular constant and assign the value and use it across the file
i am doing this - var url ; $http.get('/HomeAccessService-1/static/js/data.json').then(function(response) { url = response.data; alert(JSON.stringify(url.data)); }); //var REST_SERVICE_URI = 'localhost:8080/HomeAccessService-1/user'; var REST_SERVICE_URI = JSON.stringify(url.data);
but getting REST_SERVICE_URI is not defined at Object.fetchAllUsers in chrome console
|
0

You can do one of the two things 1. Add REST_SERVICE_URI = .. instead of that alert statement OR 2. Add those geturl call inside fetchAllUsers function and then do your next steps once you get resopnse.data

//// Add url part inside get response
var url;
var REST_SERVICE_URI;
$http.get('/HomeAccessService-1/static/js/data.json').then(function(response) {
    url = response.data;
    REST_SERVICE_URI = JSON.stringify(url.data);
});
...

//// OR do it in sync

function fetchAllUsers() {
    var deferred = $q.defer();
    $http.get('/HomeAccessService-1/static/js/data.json').then(function(response) {
      url = response.data;
      REST_SERVICE_URI = JSON.stringify(url.data);
      $http.get(REST_SERVICE_URI)
          .then(
          function (response) {
              deferred.resolve(response.data);
          },
          function(errResponse){
              console.error('Error while fetching Users');
              deferred.reject(errResponse);
          }
      );
    });
    return deferred.promise;
}

6 Comments

i am getting $scope is not defined
Did you added $scope as a dependency injection inside your code ?
Something like this---> app.controller('myCtrl', ['$scope', function($scope) { ... }]);
Ok just saw your upadated questiom where you added factory. When you call fetchAllUsers that time you dont have that 'url'
Exactly! You can do one of the two things 1. Add REST_SERVICE_URI = .. instead of that alert statement OR 2. add those geturl call inside fetchAllUsers function and then do your next steps once you get resopnse.dara
|
0
 $scope.jsondata=json.stringify(response.data)
     console.log(jsondata.url);

take all the json in one javascript variable then travarse that using loop or variable like jsondata.url and use it in ur future api call.

Comments

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.