Я хочу вернуть значение в web api обратно в файл javascript, но я не мог получить данные, может ли кто-нибудь помочь мне выяснить проблему? Я пытаюсь использовать http://localhost: 8584/api/Testing/5, чтобы проверить, есть ли какие-либо результаты или нет.
//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)
{
}
}
}
Я думаю, вам нужен обратный вызов из метода получения услуги, когда данные будут готовы. Обратите внимание, что все асинхронно, и вы можете возвращать данные только тогда, когда они доступны.
Перепишите свою службу, как показано ниже.
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;
});
}
});