c# - Angularjs $http get always return null or empty string when retrieving username from mvc controller -


this question has answer here:

i'm new angularjs , want retrieve username asp.net mvc controller. in angular's controller, have factory method "getusername" gets username mvc controller. below code

factory

appcontrollers.factory('testfactory', ['$http', function ($http) {         var factory = {};          factory.getusername = function(){             return $http.get('home/getuser').then(function (result) { return result.data; });         }          return factory;     }]); 

in app controller, called factory service "getusername" current active user. please see code , behavior.

app controller

appcontrollers.controller('contrla', ['$scope', '$http', 'testfactory', function ($scope, $http, testfactory) {      var user = ' ';         testfactory.getusername().then(function (result) {             user = result;             console.log('print out of console suser ' + user); // prints windows login name "print out of console suser amazo\demo.b"         });         console.log('print out of console user ' + user); // problem, note username missing "print out of console user"  }]); 

asp.net mvc action

 [httpget]         public string getuser()          {             var username = user.identity.name;              return username;         } 

summary: want assign result of getusername variable can use input other methods. please forgive me if question simple, i'm new angularjs , have been on issue on 4 days.

that's because http request asyncrhonus , console.log that's not working being hit before http request finishes.


Comments