本文介绍了从Angular 8调用核心方法时的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: "localhost:5000/api/File/create", ok: false, …}headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}status: 200statusText: "OK"url: "localhost:5000/api/File/create"ok: falsename: "HttpErrorResponse"message: "Http failure during parsing for 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"); }
推荐答案
有一个未解决的问题,您有很多解决方案: github/angular/angular/issues/18396
There is an opened issue and you have many solutions: github/angular/angular/issues/18396
最好的方法是更改responseType:
The best way is to change the responseType:
this.http.get(url, {responseType: 'text'})
这是参考: angular.io/guide/http#requesting-non-json-data
更多推荐
从Angular 8调用.net核心方法时的HttpErrorResponse
发布评论