mardi 27 octobre 2015

angularjs access web api through service

I want to return the value inside web api back to the javascript file, but I kept could not get the data, can anyone help me to find out the problem? I try using http://localhost:8584/api/Testing/5 to check whether there are having any result or not.

//thsi is the javascript controller who called the service.js
app.controller('mainController', function ($scope, service) {
    
    returnData();
    function returnData() {
        var getData = service.get(5);

        //the data return from web api would shown by $scope.newmessage
        getData.then(function (pl) { $scope.newmessage = pl.data },
              function (errorPl) {
                  $log.error('failure loading Employee', errorPl);
              });
    }
}
            
//this is service.js
app.service('service', function ($http) {
    
    var bseUrl = 'http://localhost:8584/';
    this.get = function (Id) {
        return $http.get(baseUrl + 'api/Testing/' + Id);
    }
});


//thsi is my web api controller
namespace angulartestOne.Controllers
{
    public class TestingController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET 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)
        {
        }
    }
}



Aucun commentaire:

Enregistrer un commentaire