1

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");
        }

2 Answers 2

2

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

The best way is to change the responseType:

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

This is the reference: https://angular.io/guide/http#requesting-non-json-data

Sign up to request clarification or add additional context in comments.

1 Comment

stackoverflow.com/a/54879401/13292987 check this answer...Also you can try to implement an Angular-Interceptor to handle every api response error, check this article: digitalocean.com/community/tutorials/… and the github sample code: github.com/vigneshsithirai/Angular-Interceptor
1

The Angular side expects a JSON, the called action method in your example returns a plain text string. For example returning this way the client side will get a JSON and it will be able to parse it:

[HttpPost("create")]
public ActionResult<string> Create([FromBody]File file)
{
    _fileService.Create(file);
    return Ok(new { str="success" });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.