[英]How do I solve ok: false response with Angular 5 and .NET Core?
有一個簡單的方法返回確定:
[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“這是正確的響應字符串”,但是在chrome控制台中有錯誤消息:
錯誤HttpErrorResponse {標頭:HttpHeaders,狀態:200,statusText:“ OK”,url:“ http:// localhost:4200 / main / name / request_name ”,確定:false,…}
這是什么意思,我該如何解決?
更新
錯誤信息:
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
以讀取響應-希望我認為您在HttpClient
中使用HttpClient
return this.http
.get<string>(`${this.apiUrl}/request_name`, {responseType: 'text'});
}.pipe(tap(res => {
console.log(res);
}));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.