我想将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)
{
}
}
}
&#13;
答案 0 :(得分:0)
我认为在数据准备就绪时,您需要使用服务的get方法进行回调。请注意,它们都是异步的,只有在可用时才能返回数据。
重新编写您的服务,如下所示
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;
});
}
});