从 Angular 8 调用 .net 核心方法时的 HttpErrorResponse [英] HttpErrorResponse when calling .net core method from Angular 8

查看:23
本文介绍了从 Angular 8 调用 .net 核心方法时的 HttpErrorResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果操作完成,我正在调用控制器方法并返回成功.但我不断收到以下错误

I'm calling a controller method and returning success if the operation is complete.But i keep getting the following error

HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:5000/api/File/create", ok: false, …}headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}status: 200statusText: "OK"url: "http://localhost:5000/api/File/create"ok: falsename: "HttpErrorResponse"message: "Http failure during parsing for http://localhost:5000/api/File/create"error: {error: SyntaxError: Unexpected token s in JSON at position 0
    at JSON.parse (<anonymous>)
    at XMLHtt…, text: "success"}__proto__: HttpResponseBase

<小时>

[HttpPost("create")]
        public ActionResult<File> Create([FromBody]File file)
        {
            _fileService.Create(file);

            return Ok("success");
        }

推荐答案

有一个未解决的问题,您有很多解决方案:https://github.com/angular/angular/issues/18396

There is an opened issue and you have many solutions: https://github.com/angular/angular/issues/18396

最好的方法是更改​​ responseType:

The best way is to change the responseType:

this.http.get(url, {responseType: 'text'})

这是参考:https://angular.io/guide/http#requesting-non-json-data

这篇关于从 Angular 8 调用 .net 核心方法时的 HttpErrorResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆