【发布时间】:2020-02-10 06:17:12
【问题描述】:
这就是问题所在:
我的 WebAPI 2.2 (ASP NET) 有一个控制器,它从关于客户端的数据库上下文信息返回。看起来是这样的:
public class ClientsController : ControllerBase
{
[HttpGet("[action]")]
public IEnumerable<Client> GetClient()
{
return _context.Client;
}
}
客户端应用程序(以角度编写)应接收数据并显示它。只要我在控制器中硬编码一些数据,它就可以正常工作,但是当它是 _context.Client 时,控制台中会抛出一个错误(如下)。
角度代码:
public clients: Client[];
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string) {
http.get<Client[]>(baseUrl + 'api/Clients/GetClient').subscribe(result => {
console.log(result);
this.clients = result;
}, error => console.error(error));
}
错误是 SyntaxError: Unexpected end of JSON input at JSON.parse () at XMLHttpRequest.l, message: Http failure during parsing for http://localhost:8080/api/Clients/GetClient
我对这个 Client[] 数组持怀疑态度,有人知道类似问题吗?
【问题讨论】:
-
看来您没有收到有效的 json。
-
是的,我认为这可能是问题所在,但我应该改变什么?
-
我测试的时候效果很好。你用的是asp.net还是asp.net core?
_context.Client有内容吗?您可以尝试返回Ok()吗?
标签: angular asp.net-core asp.net-web-api2