我正在调用一个控制器方法,如果操作为complete.But,则返回成功,我继续得到以下错误
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");
}发布于 2020-04-12 10:20:00
有一个开放的问题,您有许多解决方案:https://github.com/angular/angular/issues/18396
最好的方法是更改responseType:
this.http.get(url, {responseType: 'text'})
这是参考资料:https://angular.io/guide/http#requesting-non-json-data
发布于 2020-04-12 10:04:00
角侧需要一个JSON,在您的示例中调用的action方法返回一个纯文本字符串。例如,以这种方式返回,客户端将获得一个JSON,并且它将能够解析它:
[HttpPost("create")]
public ActionResult<string> Create([FromBody]File file)
{
_fileService.Create(file);
return Ok(new { str="success" });
}https://stackoverflow.com/questions/61168850
复制相似问题