【问题标题】:Map result in Angular 4 from Asp.Net Core来自 Asp.Net Core 的 Angular 4 映射结果
【发布时间】:2018-04-06 21:45:51
【问题描述】:

如何在我的 Angular 应用中映射来自 Asp.Net Core API 的结果?

这是我的控制器方法,它可以很好地返回数据:

[HttpGet("[action]"), Produces("application/json")]
public async Task<IActionResult> GetBooks()
{
     return Ok(new
     {
          result = await _bookService.GetBooks()
     });
}

还有我的角度分量:

export class BookComponent {
    public books: Book[];

    constructor(http: Http, @Inject('BASE_URL') baseUrl: string) {
        console.log("test");
        http.get(baseUrl + 'api/Book/GetBooks')
            .subscribe(res => {
                console.log(res.json());
            },
            error => console.error(error));
    }
}

我收到一条错误消息:

ERROR SyntaxError: Unexpected token

你知道我做错了什么吗?

更新:

console.dir(res) returns:
Response headers : Headers {_headers: Map(8), _normalizedNames:
Map(8)} ok : true status : 200 statusText : "OK" type : 2 url :
"http://localhost:63530/api/Book/GetBooks"
_body : HTML CODE
__proto__ : Body

【问题讨论】:

  • "Unexpected token Produces("application/json"),可能不是案子)。 console.dir(res) 并检查其body。里面有什么(请用它或任何其他相关信息编辑问题)?
  • @msanford 我已经更新了我的帖子。此外,我已经测试了 Swagger 的 API,它返回数据很好。
  • 谢谢。你看,_body 包含 HTML,这是你的问题。那是什么HTML?登录页面?你在不同的环境中期待什么?也许您缺少身份验证/承载令牌、标头或其他内容...
  • @msanford 这是一个新应用,我根本没有身份验证。

标签: javascript angular asp.net-web-api asp.net-core angular-http


【解决方案1】:

正如 msanford 所说,您正在返回 Html。这就是为什么 => HttpGet("[action]")。离开 HttpGet。

【讨论】:

【解决方案2】:

问题在于 URL 不正确。

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 2017-11-19
    • 2015-12-13
    • 1970-01-01
    • 2020-06-09
    相关资源
    最近更新 更多