0
votes

There is simple method returning Ok:

[HttpGet("request_name")]
public IActionResult request_name()
{
    using (var db = new WordContext())
    {
        return Ok("This is ok response string");
    }
}

And simple request in ngx:

request_name() {
    return this.http
        .get<string>(`${this.apiUrl}/request_name`);
    }.subscribe(x => {

    });

I can see code 200 "This is ok response string" in chrome, but there is error message in chrome console:

ERROR HttpErrorResponse {headers: HttpHeaders, status: 200, statusText: "OK", url: "http://localhost:4200/main/name/request_name", ok: false, …}

What does it mean and how do I solve it?


Updated

The error message:

error:
error: SyntaxError: Unexpected token T in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.onLoad (http://localhost:5010/vendor.js:7457:51) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:5010/polyfills.js:2743:31) at Object.onInvokeTask (http://localhost:5010/vendor.js:36915:33) at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:5010/polyfills.js:2742:36) at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:5010/polyfills.js:2510:47) at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:5010/polyfills.js:2818:34) at invokeTask (http://localhost:5010/polyfills.js:3862:14) at XMLHttpRequest.globalZoneAwareCallback (http://localhost:5010/polyfills.js:3888:17)
message: "Unexpected token T in JSON at position 0"
stack: "SyntaxError: Unexpected token T in JSON at position 0↵    at JSON.parse (<anonymous>)↵    at XMLHttpRequest.onLoad (http://localhost:5010/vendor.js:7457:51)↵    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:5010/polyfills.js:2743:31)↵    at Object.onInvokeTask (http://localhost:5010/vendor.js:36915:33)↵    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost:5010/polyfills.js:2742:36)↵    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost:5010/polyfills.js:2510:47)↵    at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (http://localhost:5010/polyfills.js:2818:34)↵    at invokeTask (http://localhost:5010/polyfills.js:3862:14)↵    at XMLHttpRequest.globalZoneAwareCallback (http://localhost:5010/polyfills.js:3888:17)"
__proto__: Error
text: "This is ok response string"
__proto__: Object
headers: HttpHeaders
lazyInit: ƒ ()
lazyUpdate: null
normalizedNames: Map(0) {}
__proto__: Object
message: "Http failure during parsing for http://localhost:5005/main/word/request_name"
name: "HttpErrorResponse"
ok: false
status: 200
statusText: "OK"
url: "http://localhost:5005/main/word/request_name"
1
what is the complete error log? - Jota.Toledo
@Jota.Toledo defaultErrorLogger @ core.js:1673 push../node_modules/@angular/core/fesm5/core.js.ErrorHandler.handleError @ core.js:1719 next @ core.js:4311 schedulerFn @ core.js:3551 push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.__tryOrUnsub @ Subscriber.js:196 push../node_modules/rxjs/_esm5/internal/Subscriber.js.SafeSubscriber.next @ Subscriber.js:134 push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next @ Subscriber.js:77 push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next @ Subscriber.js:54 and so on - mr_blond
Not the stack trace. I mean, you got ERROR: HttpErrorResponse ..., which are the details of an error object. Could you update your question with the complete object details? - Jota.Toledo
Ah, I see. I've added details to question. - mr_blond
The error instance is clear, the HttpClient tries to parse an invalid JSON string. See angular.io/guide/http#requesting-non-json-data - Jota.Toledo

1 Answers

0
votes

If your response type is not a JSON it will just pass the response to the error

Try something like this

request_name() {
    return this.http
        .get<string>(`${this.apiUrl}/request_name`, {responseType: 'text'});
    }.subscribe(x => {

    });

Whereas don't use subscribe use pipe operator and tap to read the response - hope i think you are using HttpClient in angular

    return this.http
            .get<string>(`${this.apiUrl}/request_name`, {responseType: 'text'});
        }.pipe(tap(res => {
           console.log(res);
        }));