javascript - angularjs access web api through service -
i want return value inside web api javascript file, kept not data, can me find out problem? try using http://localhost:8584/api/testing/5 check whether there having result or not.
//thsi javascript controller called service.js app.controller('maincontroller', function ($scope, service) { returndata(); function returndata() { var getdata = service.get(5); //the data return web api shown $scope.newmessage getdata.then(function (pl) { $scope.newmessage = pl.data }, function (errorpl) { $log.error('failure loading employee', errorpl); }); } } //this service.js app.service('service', function ($http) { var bseurl = 'http://localhost:8584/'; this.get = function (id) { return $http.get(baseurl + 'api/testing/' + id); } }); //thsi web api controller namespace angulartestone.controllers { public class testingcontroller : apicontroller { // api/<controller> public ienumerable<string> get() { return new string[] { "value1", "value2" }; } // api/<controller>/5 public string get(int id) { return "value"; } // post api/<controller> public void post([frombody]string value) { } // put api/<controller>/5 public void put(int id, [frombody]string value) { } // delete api/<controller>/5 public void delete(int id) { } } }
i think need callback service's method when data ready. please note it's asynchronous , can return data when available.
re-write service below
app.service('service', function ($http) { var bseurl = 'http://localhost:8584/'; this.get = function (id) { $http.get(baseurl + 'api/testing/' + id) .then(function (response) { return response.data; }); } });
Comments
Post a Comment