如何解决 ok: false response with Angular 5 和 .NET Core?

How do I solve ok: false response with Angular 5 and .NET Core?

有简单的方法返回OK:

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

以及 ngx 中的简单请求:

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

    });

我可以在 chrome 中看到代码 200 "This is ok response string",但 chrome 控制台中有错误消息:

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

这是什么意思,我该如何解决?


Updated

错误信息:

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"

如果您的响应类型不是 JSON,它只会将响应传递给错误

尝试这样的事情

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

    });

而不要使用 subscribe 使用 pipe 运算符和 tap 来读取响应 - 希望我认为您在 angular 中使用 HttpClient

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